Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple lines, in different colors, with pandas dataframe

I have a dataframe that looks like the following

   color  x   y
0    red  0   0
1    red  1   1
2    red  2   2
3    red  3   3
4    red  4   4
5    red  5   5
6    red  6   6
7    red  7   7
8    red  8   8
9    red  9   9
10  blue  0   0
11  blue  1   1
12  blue  2   4
13  blue  3   9
14  blue  4  16
15  blue  5  25
16  blue  6  36
17  blue  7  49
18  blue  8  64
19  blue  9  81

I ultimately want two lines, one blue, one red. The red line should essentially be y=x and the blue line should be y=x^2

When I do the following:

df.plot(x='x', y='y')

The output is this:

Is there a way to make pandas know that there are two sets? And group them accordingly. I'd like to be able to specify the column color as the set differentiator

like image 613
sedavidw Avatar asked Mar 24 '15 12:03

sedavidw


People also ask

How do you plot multiple lines in a DataFrame in Python?

You can plot multiple lines from the data provided by a Dataframe in python using matplotlib. You can do it by specifying different columns of the dataframe as the x and y-axis parameters in the matplotlib. pyplot. plot() function.

How do you plot different data on the same graph in Python?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.

How do you plot multiple lines in Seaborn?

You probably need to re-organize your dataframe in a suitable way so that there is one column for the x data, one for the y data, and one which holds the label for the data point. You can also just use matplotlib. pyplot . If you import seaborn , much of the improved design is also used for "regular" matplotlib plots.


3 Answers

Another simple way is to use the pandas.DataFrame.pivot function to format the data.

Use pandas.DataFrame.plot to plot. Providing the colors in the 'color' column exist in matplotlib: List of named colors, they can be passed to the color parameter.

# sample data
df = pd.DataFrame([['red', 0, 0], ['red', 1, 1], ['red', 2, 2], ['red', 3, 3], ['red', 4, 4], ['red', 5, 5], ['red', 6, 6], ['red', 7, 7], ['red', 8, 8], ['red', 9, 9], ['blue', 0, 0], ['blue', 1, 1], ['blue', 2, 4], ['blue', 3, 9], ['blue', 4, 16], ['blue', 5, 25], ['blue', 6, 36], ['blue', 7, 49], ['blue', 8, 64], ['blue', 9, 81]], columns=['color', 'x', 'y'])

# pivot the data into the correct shape
df = df.pivot(index='x', columns='color', values='y')

# display(df)
color  blue  red
x               
0         0    0
1         1    1
2         4    2
3         9    3
4        16    4
5        25    5
6        36    6
7        49    7
8        64    8
9        81    9

# plot the pivoted dataframe; if the column names aren't colors, remove color=df.columns
df.plot(color=df.columns, figsize=(5, 3))

enter image description here

like image 56
MrE Avatar answered Oct 06 '22 20:10

MrE


You could use groupby to split the DataFrame into subgroups according to the color:

for key, grp in df.groupby(['color']):

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

df = pd.read_table('data', sep='\s+')
fig, ax = plt.subplots()

for key, grp in df.groupby(['color']):
    ax = grp.plot(ax=ax, kind='line', x='x', y='y', c=key, label=key)

plt.legend(loc='best')
plt.show()

yields enter image description here

like image 88
unutbu Avatar answered Oct 06 '22 22:10

unutbu


If you have seaborn installed, an easier method that does not require you to perform pivot:

import seaborn as sns

sns.lineplot(data=df, x='x', y='y', hue='color')
like image 24
Cheng Avatar answered Oct 06 '22 21:10

Cheng