我正在尝试让 Pandas 覆盖条形图和线条图。这两个系列的比例不同,因此我希望将值绘制在两个“y”轴上。我不能让 Pandas 一起显示“条形图”和“线形图”。
from pandas import DataFrame
df_eg = DataFrame()
df_eg=DataFrame(data=[(1212,231),(9283,624),(11734,943),(12452,1037),(16766,1037),(120,113)],index=[2014,2015,2016,2017,2018,2019],columns=["Release","Hold"])
这给出了DataFrame
Release Hold
2014 1212 231
2015 9283 624
2016 11734 943
2017 12452 1037
2018 16766 1037
2019 120 113
现在,如果我尝试将“发布”绘制为条形图,将“保持”列绘制为具有双轴的线,则只能得到该线。
fig, ax = plt.subplots()
ax2 = ax.twinx()
plt.hold(False)
df_eg["Release"].plot(ax=ax,kind="bar")
df_eg["Hold"].plot(ax=ax2, style='r-', secondary_y=True)
ax.legend(loc='best')
但是,如果我将两者都绘制为线。这两个值都显示出来。
我想知道如何使条形图和线形图显示在同一图上。我正在使用pandas版本'0.16.2'和matplotlib版本'1.3.1'。
fig, ax = plt.subplots()
ax2 = ax.twinx()
plt.hold(False)
df_eg["Release"].plot(ax=ax,kind="line")
df_eg["Hold"].plot(ax=ax2, style='r-', secondary_y=True)
ax.legend(loc='best')
最佳答案
这样可以解决您的问题吗?
fig, ax = plt.subplots()
ax2 = ax.twinx()
ax.bar(df_eg.index, df_eg["Release"], color=(190/255,190/255,190/255,0.7), label='Release')
ax2.plot(df_eg.index, df_eg["Hold"], color='green', label='Hold')
ax.set_xticklabels(df_eg.index)
ax.legend(loc='best')
关于python - pandas DataFrame如何混合不同比例的条形图和折线图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33457861/