使用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')
然后我得到:
![with axlines](https://istack.dev59.com/k3Uv7.webp)
由于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')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.yaxis.tick_left()
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()
![with_spines](https://istack.dev59.com/KYDMJ.webp)
使用 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)
![with_seaborn](https://istack.dev59.com/FHALV.webp)
使用脊柱的set_position
方法
这里是关于spines的set_position
方法的文档:
脊柱位置由2元组(位置类型,数量)指定。位置类型包括:
此外,速记符号定义了特殊位置:
- 'center' -> ('axes',0.5)
- 'zero' -> ('data', 0.0)
因此,您可以使用以下代码将左侧脊柱放置在任何位置:
ax.spines['left'].set_position((system, poisition))
其中system
可以是'outward'、'axes'或者'data',而position
则是在该坐标系中的位置。