Python 更改 matplotlib 轴设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:05:45  来源:igfitidea点击:

change matplotlib axis settings

pythonmatplotlibscipy

提问by Falmarri

How do I get control over the axis settings of a pyplot plot. I have simply done

如何控制 pyplot 图的轴设置。我只是做了

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

    pylab.show()

and I get this which is what I want

我得到了这就是我想要的

alt text

替代文字

but I want the x axis to be at 0 instead of at the bottom. How would I do that?

但我希望 x 轴位于 0 而不是底部。我该怎么做?

采纳答案by doug

# 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()

alt text

替代文字

回答by amillerrhodes

To set start of x-axis to 0:

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

pylab.xlim(xmin=0)

To set start of y-axis to 0:

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

pylab.ylim(ymin=0)

Put one of these lines (or both if you'd like) after the pylab.plotcall.

pylab.plot通话后放置其中一行(如果您愿意,也可以放置两行)。