如何根据来自多列的数据在Pandas Python中的一张图中绘制多条线? [英] How to plot multiple lines in one figure in Pandas Python based on data from multiple columns?
本文介绍了如何根据来自多列的数据在Pandas Python中的一张图中绘制多条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含3列的数据框,如下所示:
I have a dataframe with 3 columns, like this:
df['year'] = ['2005, 2005, 2005, 2015, 2015, 2015, 2030, 2030, 2030']
df['name'] = ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
df['weight'] = [80, 65, 88, 65, 60, 70, 60, 55, 65]
我如何绘制A,B和C的线,以显示它们多年来的重量变化.所以我尝试了这个:
how can I plot a line for A, B and C, where it shows how their weight develops through the years. So I tried this:
df.groupby("euro").plot(x="year", y="MKM")
但是,我得到多个地块,而这不是我想要的.我希望所有这些图都在一个图中.
However, I get multiple plots and that is not what I want. I want all those plots in one figure.
推荐答案
这会产生您想要的东西吗?
Does this produce what you're looking for?
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
for name in ['A','B','C']:
ax.plot(df[df.name==name].year,df[df.name==name].weight,label=name)
ax.set_xlabel("year")
ax.set_ylabel("weight")
ax.legend(loc='best')
这篇关于如何根据来自多列的数据在Pandas Python中的一张图中绘制多条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文