I have a dataframe where I want to create a line graph. I want one line per category and on the x-axis I want the date, and on the y-axis the 12MKG. I don’t know how to put labels on the lines and make the lines different colours per category.
So far I have tried:
plt.plot(df['date'], datatoplot['12MKG'])
date | categorie | 12MKG |
---|---|---|
202001 | cat1 | 0.9836956 |
202002 | cat1 | 0.9836956 |
202003 | cat1 | 0.9831461 |
202004 | cat1 | 0.97206706 |
202005 | cat1 | 0.9698492 |
202006 | cat1 | 0.97630334 |
202007 | cat1 | 0.9787234 |
202008 | cat1 | 0.9810606 |
202009 | cat1 | 0.9825784 |
202010 | cat1 | 0.98165137 |
202011 | cat1 | 0.9768116 |
202012 | cat1 | 0.96666664 |
202101 | cat1 | 0.9655172 |
202102 | cat1 | 0.95214105 |
202103 | cat1 | 0.93721974 |
202104 | cat1 | 0.9419087 |
202105 | cat1 | 0.93158954 |
202106 | cat1 | 0.9189189 |
202107 | cat1 | 0.9144603 |
202001 | cat2 | 0.3118644 |
202002 | cat2 | 0.3006993 |
202003 | cat2 | 0.3017544 |
202004 | cat2 | 0.29433963 |
202005 | cat2 | 0.3030303 |
202006 | cat2 | 0.30483273 |
202007 | cat2 | 0.33206108 |
202008 | cat2 | 0.33730158 |
202009 | cat2 | 0.344 |
202010 | cat2 | 0.34008098 |
202011 | cat2 | 0.34051725 |
202012 | cat2 | 0.3224299 |
202101 | cat2 | 0.33027524 |
202102 | cat2 | 0.3187773 |
202103 | cat2 | 0.29338843 |
202104 | cat2 | 0.28458497 |
202105 | cat2 | 0.2804878 |
202106 | cat2 | 0.2804878 |
202107 | cat2 | 0.2631579 |
You can use colormap to get different colors for each line and df.loc to filter each category
import matplotlib.pylab as pl
import matplotlib.pyplot as plt
labels = set(df['categorie'].values)
colors = pl.cm.jet(np.linspace(0,1,len(labels)))
for key, color in zip(labels, range(len(labels))):
data_x = df.loc[df['categorie']==key]['date']
data_y = df.loc[df['categorie']==key]['12MKG']
plt.plot(data_x, data_y, color=colors[color], label=key)
plt.legend()
plt.show()
See here for more details on colormaps