我正在尝试创建堆积面积图,以显示课程及其数量随时间的变化。所以我的数据框是(index = Year):
Area Courses
Year
1900 Agriculture 0.0
1900 Architecture 32.0
1900 Astronomy 10.0
1900 Biology 20.0
1900 Chemistry 25.0
1900 Civil Engineering 21.0
1900 Education 14.0
1900 Engineering Design 10.0
1900 English 30.0
1900 Geography 1.0
去年:2011。
我尝试了几种解决方案,例如df.plot.area(),df.plot.area(x ='Years')。然后,我认为将“区域”作为列会有所帮助,因此我尝试了
df.pivot_table(index = 'Year', columns = 'Area', values = 'Courses', aggfunc = 'sum')
但是我得到的不是每年的课程总数,而是:
Area Aeronautical Engineering ... Visual Design
Year ...
1900 NaN ... NaN
1901 NaN ... NaN
感谢您的帮助。这是我的第一篇文章。对不起,如果我错过了什么。
也许您想使用plt.stackplot()
。这是一个简单的示例。
import matplotlib.pyplot as plt
x=range(1,6)
y=[ [1,4,6,8,9], [2,2,7,10,12], [2,8,5,10,6] ]
plt.stackplot(x,y, labels=['A','B','C'])
plt.legend(loc='upper left')
plt.show()
您可以在这里找到更多示例:https://python-graph-gallery.com/stacked-area-plot/。