2017-03-22 68 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)