Python Pandas Matplotlib Plot Color value of type defined in one column

I have data in the following format:

import pandas as ps
table={'time':[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5],\
    'data':[1,1,2,2,2,1,2,3,4,5,1,2,2,2,3],\
    'type':['a','a','a','a','a','b','b','b','b','b','c','c','c','c','c']}
df=ps.DataFrame(table,columns=['time','data','type']

      

I would like to plot the data as a function of the time associated with the line, but I would like each line to be a separate color for unique types. In this example, the result will be three lines: a data line (time) for each type a, b, and c. Any guidance is appreciated.

I was unable to create a row with this data - pandas.scatter will produce a graph, but pandas.plot will not. I've been messing around with loops to create a plot for each type, but I haven't found a direct way to do this. My data usually has an unknown number of unique types. Does pandas and / or matpltlib have a way to create this type of plot?

+3


source to share


2 answers


Pandas' build capabilities allow you to do this if everything is indexed correctly. However, sometimes it's easier to just use matplotlib:

import pandas as pd
import matplotlib.pyplot as plt

table={'time':[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5],
       'data':[1,1,2,2,2,1,2,3,4,5,1,2,2,2,3],
       'type':['a','a','a','a','a','b','b','b','b','b','c','c','c','c','c']}
df=pd.DataFrame(table, columns=['time','data','type'])

groups = df.groupby('type')

fig, ax = plt.subplots()
for name, group in groups:
    ax.plot(group['time'], group['data'], label=name)
ax.legend(loc='best')

plt.show()

      

enter image description here



If you prefer to use a wrapper pandas

, you need to override the legend labels:

import pandas as pd
import matplotlib.pyplot as plt

table={'time':[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5],
       'data':[1,1,2,2,2,1,2,3,4,5,1,2,2,2,3],
       'type':['a','a','a','a','a','b','b','b','b','b','c','c','c','c','c']}
df=pd.DataFrame(table, columns=['time','data','type'])

df.index = df['time']
groups = df[['data', 'type']].groupby('type')

fig, ax = plt.subplots()
groups.plot(ax=ax, legend=False)
names = [item[0] for item in groups]
ax.legend(ax.lines, names, loc='best')

plt.show()

      

enter image description here

+3


source


Just choose a seaborn solution.

import seaborn as sns
import matplotlib.pyplot as plt

g = sns.FacetGrid(df, hue="type", size=5)
g.map(plt.plot, "time", "data")
g.add_legend()

      



enter image description here

+1


source







All Articles