2013-05-15 45 views
1

我有一个图表,创建于pandas,其中我已将y轴设置为-100至-100。简单的方法来设置熊猫x轴的位置?

是否有一种简单的方法让x轴在y = 0处与y轴交叉,而不是在y = -100处交叉(或者,如何在垂直中心处显示x轴,而不是在图表底部)

我见过的解决方案似乎使用子图或spines,这似乎对我的目的过于复杂。我期待更多的东西整合与大熊猫一样,经过ylimstyle参数)

示例代码:

from pandas import Series 
s=Series([-25,0,70]) 
s.plot(ylim=(-100,100)) 

chart with x-axis at the bottom

+0

你可以发布您的代码,请,它只是更容易理解你的问题,即使它是清楚自己想要 –

+0

你可以什么在图的中间手动绘制一条线:http://stackoverflow.com/questions/5394527/matplotlib-how-to-draw-an-axis-in-the-middle-of-the-figure。但似乎matplotlib没有将xaxis.tick发送到图的中间的命令。 (有'ax.xaxis.tick_bottom'和'ax.xaxis.tick_top',但不是中间的] – joon

+0

据我所知,'pandas'中没有这样的功能,因为'pandas'的绘图功能非常强大与'matplotlib'相比有限。此外,如果您希望将x轴的tick标记/ tickmarks连接到“中轴”(同时平移/缩放),则最容易插入额外的脊柱;请查看['mpl_toolkits.axisartist'](http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#axisartist)以查看其中的一些示例。 – hooy

回答

1

的解决方案,我至今确实使用次要情节:

from pandas import Series 
s=Series([-25,0,70]) 

import matplotlib.pyplot as plt 
fig=plt.figure() 
ax=fig.add_subplot(111) 
ax.set_ylabel('percentage') 
ax.spines['bottom'].set_position('zero')  # x-axis where y=0 
#ax.spines['bottom'].set_position('center')  # x-axis at center (not necessarily y=0) 
#ax.spines['bottom'].set_position(('data', 50)) # x-axis where y=50 
ax.spines['top'].set_color('none')    # hide top axis 
ax.spines['right'].set_color('none')   # hide right axis 

s.plot(ylim=(-100,100)) 

chart with centered x-axis

不知道为什么没有显示在底部的网格线,而不是我的问题

+1

当我运行你的代码时,底部的网格线实际上被显示(matplotlib 1.2.1)。在这种情况下,我会推荐使用'ax.spines ['bottom'] .set_position('zero')'而不是'ax.spines ['bottom'] .set_position('center')',因为后者放置脊柱位于“轴线”的中心,不一定在y = 0处,这意味着它在平移/缩放时相对于y轴移动。只有当y-限制关于y = 0对称时,这些才是类似的。此外,您可以使用'ax.tick_params(top = False,right = False)“来删除顶部和右侧的”浮动“标记,以获得更清晰的外观。 – hooy

+0

感谢您的“零”建议,我更新了代码。 – Rabarberski

+0

包含导入用于完成代码示例的'plt'命名空间 – mtadd