使用subplots 不是太复杂,可能是刺。
愚蠢,简单的方法:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
我得到:
(您看不到垂直轴,因为 x 下限为零。)
使用简单刺的替代方法
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
# set the x-spine (see below for more info on `set_position`)
ax.spines['left'].set_position('zero')
# turn off the right spine/ticks
ax.spines['right'].set_color('none')
ax.yaxis.tick_left()
# set the y-spine
ax.spines['bottom'].set_position('zero')
# turn off the top spine/ticks
ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()
替代使用seaborn(我最喜欢的)
import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set(style='ticks')
x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
seaborn.despine(ax=ax, offset=0) # the important part here
使用spine的set_position方法
这是set_position method of spines 的文档:
脊椎位置由(位置类型,数量)的 2 个元组指定。
职位类型有:
此外,速记符号定义了一个特殊的位置:
- 'center' -> ('axes',0.5)
- “零”->(“数据”,0.0)
所以你可以在任何地方放置,比如左脊椎:
ax.spines['left'].set_position((system, poisition))
其中system 是“向外”、“轴”或“数据”,position 在该坐标系中的位置。