Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple line graph using pandas and matplotlib

Tags:

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.

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.

Is it possible to do this using matplotlib?

like image 403
Sudar Avatar asked Jun 06 '14 11:06

Sudar


People also ask

How do you make a multiple line graph on pandas?

Set the figure size and adjust the padding between and around the subplots. Make a 2D potentially heterogeneous tabular data using Pandas DataFrame class, where the column are x, y and equation. Get the reshaped dataframe organized by the given index such as x, equation, and y. Use the plot() method to plot the lines.


2 Answers

You can use the groupby method:

data.groupby("template").plot(x="date", y="score")
like image 81
Thomas Cokelaer Avatar answered Oct 12 '22 16:10

Thomas Cokelaer


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()
like image 25
Jon Hoffman Avatar answered Oct 12 '22 15:10

Jon Hoffman