2010-11-27 97 views
9

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

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

    pylab.show() 

,我得到这个这是我想

alt text

,但我想x轴是在0,而不是在底部。我会怎么做?

回答

14
# 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

+0

这看起来像我需要什么,以@Joe金顿的评论 – Falmarri 2010-11-29 18:42:19

+0

沿着你可以找到如何在这方面,这里使用的刺更一般的描述:http://matplotlib.org/examples/pylab_examples/spine_placement_demo .html – 2014-10-29 17:14:12

10

要设置x轴开始到0:

pylab.xlim(xmin=0) 

要设置y轴的开始到0:

pylab.ylim(ymin=0) 

将这些中的一个在pylab.plot电话后面(或者如果你愿意的话)。

1

我觉得这是你想要

alt text

什么,但我的代码是非常脏。它必须仔细调整。 为什么不直接使用grid

import pylab 
import numpy as np 

x = np.arange(20)+300 
y = x - 308 

x1 = x[y>=0] 
x2 = x[y<=0] 
y1 = y[y>=0] 
y2 = y[y<=0] 


fig = pylab.figure() 
fig.subplots_adjust(hspace=0.00001) 
bx = pylab.subplot(212) 
pylab.ylim(-12,0) 
pylab.plot(x2,y2) 
pylab.setp(bx.get_xticklabels(), visible=False) 
ax = pylab.subplot(211,sharex=bx) 
pylab.plot(x1,y1) 
pylab.ylim(0,12) 
pylab.setp(ax.get_xticklabels(), visible=True) 
pylab.show()