2017-03-22 70 views
0

我想从下面的例子中用线条图覆盖堆积的条形图,但只显示第二个阴谋,无法理解为什么。熊猫的阴谋不覆盖

import pandas as pd 
from matplotlib import pyplot as plt 
df=pd.DataFrame({'yarding, mobile cable yarder on trailer': {1928: 1.4027824821879459e-20, 1924: 3.4365045943961052e-37, 1925: 6.9939032596152882e-30, 1926: 1.0712940173393567e-25, 1927: 8.6539917152671678e-23}, 
       'yarding and processing, mobile cable yarder on truck': {1928: 1.1679873528237404e-20, 1924: 2.8613089094435456e-37, 1925: 5.8232768671842113e-30, 1926: 8.9198283644271726e-26, 1927: 7.2055027953028907e-23}, 
       'delimbing, with excavator-based processor': {1928: 1.6998969986716558e-20, 1924: 4.1643685881703105e-37, 1925: 8.4752370448040848e-30, 1926: 1.2981979323251926e-25, 1927: 1.0486938381883222e-22}}) 
df2=pd.Series({1928: 3.0638184091973243e-19, 1924: 7.5056562764093482e-36, 1925: 1.5275356821475311e-28, 1926: 2.3398091372066067e-24, 1927: 1.8901157781841223e-21}) 

ax=df.plot(kind='bar',stacked=True,legend=False) 
df2.plot(kind='line',ax=ax) 
plt.show() 

enter image description here

+0

你没有看到,因为酒吧它的最大值是4.5×10^-20。 – Serenity

+0

我曾试图做df = df * 1e + 26和df2 = df2 * 1e + 26作为测试,但概率仍然存在。在任何情况下,如果我分别绘制两个数据框是好的,那么可能是它们不重叠 –

回答

2

线图将数值数据相互绘制。
条形图将数字数据与分类数据进行对比。因此,即使条形图中的x值是数字,它们绘制的刻度也不对应于这些数字,而是与某些索引相对应。

这意味着条形图的x轴比例总是从0到N,其中N是条的数量(粗略地说,实际上它相当于-0.5到N-0.5)。

如果您现在在1000以上的范围内添加一些值,那么这些小节会缩小,直到它们不再可见(因此您可能认为它们甚至不在此处)。

为了规避这个问题,你可以在两个不同的轴上工作。一个用于线条图,一个用于条形图,但让他们共享相同的y轴。

下面是一个可能的解决方案(这是非常相似,从马丁,解决他我打字这一点的同时还增加):

import pandas as pd 
from matplotlib import pyplot as plt 
df=pd.DataFrame({'yarding, mobile cable yarder on trailer': {1928: 1.4027824821879459e-20, 1924: 3.4365045943961052e-37, 1925: 6.9939032596152882e-30, 1926: 1.0712940173393567e-25, 1927: 8.6539917152671678e-23}, 
       'yarding and processing, mobile cable yarder on truck': {1928: 1.1679873528237404e-20, 1924: 2.8613089094435456e-37, 1925: 5.8232768671842113e-30, 1926: 8.9198283644271726e-26, 1927: 7.2055027953028907e-23}, 
       'delimbing, with excavator-based processor': {1928: 1.6998969986716558e-20, 1924: 4.1643685881703105e-37, 1925: 8.4752370448040848e-30, 1926: 1.2981979323251926e-25, 1927: 1.0486938381883222e-22}}) 
df2=pd.Series({1928: 3.0638184091973243e-19, 1924: 7.5056562764093482e-36, 1925: 1.5275356821475311e-28, 1926: 2.3398091372066067e-24, 1927: 1.8901157781841223e-21}) 

fig, ax = plt.subplots() 
# optionally make log scale 
ax.set_yscale("log", nonposy='clip') 
# create shared y axes 
ax2 = ax.twiny() 
df.plot(kind='bar',stacked=True,legend=False, ax=ax) 
df2.plot(kind='line',ax=ax2) 
ax2.xaxis.get_major_formatter().set_useOffset(False) 
# remove upper axis ticklabels 
ax2.set_xticklabels([]) 
# set the limits of the upper axis to match the lower axis ones 
ax2.set_xlim(1923.5,1928.5) 
plt.show() 

enter image description here

2

您可以使用ax.twiny()secondary_y=True如下:

import pandas as pd 
from matplotlib import pyplot as plt 

df = pd.DataFrame({'yarding, mobile cable yarder on trailer': {1928: 1.4027824821879459e-20, 1924: 3.4365045943961052e-37, 1925: 6.9939032596152882e-30, 1926: 1.0712940173393567e-25, 1927: 8.6539917152671678e-23}, 
       'yarding and processing, mobile cable yarder on truck': {1928: 1.1679873528237404e-20, 1924: 2.8613089094435456e-37, 1925: 5.8232768671842113e-30, 1926: 8.9198283644271726e-26, 1927: 7.2055027953028907e-23}, 
       'delimbing, with excavator-based processor': {1928: 1.6998969986716558e-20, 1924: 4.1643685881703105e-37, 1925: 8.4752370448040848e-30, 1926: 1.2981979323251926e-25, 1927: 1.0486938381883222e-22}}) 
df2 = pd.Series({1928: 3.0638184091973243e-19, 1924: 7.5056562764093482e-36, 1925: 1.5275356821475311e-28, 1926: 2.3398091372066067e-24, 1927: 1.8901157781841223e-21}) 

fig, ax = plt.subplots() 
ax2 = ax.twiny() 
df.plot(kind='bar', stacked=True, legend=False, ax=ax) 
df2.plot(kind='line', secondary_y=True) 
plt.show()  

这将使你:

two shared pandas plots

你可能需要调整labellin g,例如:

ax2.get_xaxis().set_visible(False)