更改matplotlib轴设置

Fal*_*rri 9 python matplotlib scipy

如何控制pyplot图的轴设置.我完成了

    pylab.plot(*self.plot_generator(low, high))

    pylab.show()
Run Code Online (Sandbox Code Playgroud)

我得到的就是我想要的东西

替代文字

但我希望x轴为0而不是底部.我该怎么办?

dou*_*oug 18

# create some data
x = np.linspace(-np.pi,np.pi,100)
y = np.cos(2.5*x)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, mfc='orange', mec='orange', marker='.')

# using 'spines', new in Matplotlib 1.0
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

ax.axhline(linewidth=2, color='blue')
ax.axvline(linewidth=2, color='blue')
show()
Run Code Online (Sandbox Code Playgroud)

替代文字

  • 你能解释为什么在这里使用`ax.spines`.AFAIK,所有方法`set_position`或`set_color`都是`matplotlib.spines.Spine`类对象的成员.这令人困惑. (2认同)

ami*_*des 11

要将x轴的起点设置为0:

pylab.xlim(xmin=0)
Run Code Online (Sandbox Code Playgroud)

要将y轴的起点设置为0:

pylab.ylim(ymin=0)
Run Code Online (Sandbox Code Playgroud)

pylab.plot通话结束后,将其中一行(或两者,如果您愿意)放入其中.