Python:在X轴上带有年份的折线图和条形图?

时间:2020-08-11 23:01:23

标签: python pandas

我试图在X轴上显示年份,用条形表示的体积,用线形表示的值。谁能看到我要去哪里错了?

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

data1 = pd.DataFrame({'Year' : [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019],
                         'Volume' : [32, 35, 33, 36, 38, 39, 40, 42, 49, 47],
                         'Value' : [40, 41, 46, 44, 43, 42, 42, 45, 48, 52]})

data1[['Year', 'Volume']].plot(kind='bar', width = 0.5, color="navy")
data1['Value'].plot(secondary_y=True, color='orange')

plt.show()

1 个答案:

答案 0 :(得分:1)

您要将X='Year'传递到绘图命令中。

data1[['Year', 'Volume']].plot(x='Year',kind='bar', width = 0.5, color="navy")
data1['Value'].plot(secondary_y=True, color='orange')

输出:

enter image description here