Python 使用 pandas 和 matplotlib 绘制多线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24080275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting multiple line graph using pandas and matplotlib
提问by Sudar
I have the following data in a pandas dataframe
我在熊猫数据框中有以下数据
date template score
0 20140605 0 0.138786
1 20140605 1 0.846441
2 20140605 2 0.766636
3 20140605 3 0.259632
4 20140605 4 0.497366
5 20140606 0 0.138139
6 20140606 1 0.845320
7 20140606 2 0.762876
8 20140606 3 0.261035
9 20140606 4 0.498010
For every day there will be 5 templates and each template will have a score.
每天将有 5 个模板,每个模板都有一个分数。
I want to plot the date in the x axis and score in the y axis and a separate line graph for each template in the same figure.
我想在 x 轴上绘制日期并在 y 轴上绘制分数,并为同一图中的每个模板绘制一个单独的折线图。
Is it possible to do this using matplotlib?
是否可以使用 matplotlib 来做到这一点?
采纳答案by papafe
You can use an approach like the following one. You can simply slice the dataframe according to the values of each template, and subsequently use the dates and scores for the plot.
您可以使用如下方法。您可以简单地根据每个模板的值对数据框进行切片,然后使用绘图的日期和分数。
from pandas import *
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
#The following part is just for generating something similar to your dataframe
date1 = "20140605"
date2 = "20140606"
d = {'date': Series([date1]*5 + [date2]*5), 'template': Series(range(5)*2),
'score': Series([random() for i in range(10)]) }
data = DataFrame(d)
#end of dataset generation
fig, ax = plt.subplots()
for temp in range(5):
dat = data[data['template']==temp]
dates = dat['date']
dates_f = [dt.datetime.strptime(date,'%Y%m%d') for date in dates]
ax.plot(dates_f, dat['score'], label = "Template: {0}".format(temp))
plt.xlabel("Date")
plt.ylabel("Score")
ax.legend()
plt.show()
回答by Thomas Cokelaer
You can use the groupby method:
您可以使用 groupby 方法:
data.groupby("template").plot(x="date", y="score")
回答by animalito
You can add the legend according to the groups with:
您可以根据组添加图例:
plt.legend(pr['template'], loc='best')
回答by Jon Hoffman
I think the easiest way to plot this data with all the lines on the same graph is to pivot it such that each "template" value is a column:
我认为用同一图形上的所有线绘制此数据的最简单方法是将其旋转,使每个“模板”值都是一列:
pivoted = pandas.pivot_table(data, values='score', columns='template', index='date')
# Now there will be an index column for date and value columns for 0,1,2,3,4
pivoted.plot()