Dmitry Polonskiy Dmitry Polonskiy - 23 days ago 8
Python Question

Plotting N number of Graphs

I have a DataFrame that looks like this:

spot total
date_delivery
2016-06-21 x 20
2016-07-25 x 22
2016-08-14 x 25
2016-09-11 y 16
2016-10-16 y 10


The index of the DataFrame is in a
datetime
format. I want to create a simple graph for each unique spot that shows the total over time. I am having trouble writing a loop that performs this as well as saves each one. Keep in mind that while there is only 2 actual
spots
in this DataFrame the real one has many many more.

Answer

Append spot to the index, groupby spot and then plot

df.set_index('spot', append=True).groupby(level='spot').plot(kind='bar')

For your example you'll get two bar graphs, one for x, one for y, right below each other (but you can customize that)

Comments