Pandas的情节没有重叠

2024-09-26 22:52:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用下面例子中的折线图覆盖堆积条形图,但只显示了第二个图,无法理解原因。在

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


Tags: importdfplotonaspltmobileax
2条回答

折线图将数字数据相互对照。
条形图根据分类数据绘制数字数据。因此,即使条形图中的x值是数字,绘制它们的比例与这些数字并不对应,而是与某些索引相对应。在

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

如果现在将1000以上范围内的某些值添加到该刻度上,则条形图将缩小,直到无法再看到它们为止(因此,您可能认为它们根本不存在)。在

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

以下是一个可能的解决方案(与Martin的解决方案非常相似,他在我键入此内容时添加了该方案):

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

您可以使用ax.twiny()和{},如下所示:

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

您可能需要根据需要调整标签,例如:

^{pr2}$

相关问题 更多 >

    热门问题