2

I want to create a multi series line plot showing how the occurrences of a data-frame element change over time:

I have two lists in which I have joined in a dataframe:

df = pd.DataFrame(
{'Date': datelist,
 'Category': catlist
})

I have then grouped the dataframe to show the counts of each occurrence over time:

df = df.groupby(['Date', 'Category']).size()
print df

This returns something that looks like this:

 Date        Category        
 13/02/2018  clean          2
             suspicious     1
 14/02/2018  clean          2
 19/02/2018  clean          2

I now want to create a multi-series line plot for each category type, showing how the count changes over date.

I'm really not sure how to do this using matplotlib

2 Answers 2

2

You can try with:

plot_df = df.unstack('Category')
plot_df.index = pd.PeriodIndex(plot_df.index.tolist(), freq='D')
plot_df.plot()
plt.show()

Or with subplot:

plot_df.plot(subplots=True)

Example: For the dataframe:

Date        Category
13/02/2018  clean
13/02/2018  clean
13/02/2018  suspicious
14/02/2018  clean
14/02/2018  clean
19/02/2018  clean
19/02/2018  clean
14/02/2018  suspicious
13/02/2018  suspicious
14/02/2018  clean

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, is there a reason why it only plots the one category and only plots a single date from the list?
What do you mean? It can happen that if a category is constant, you dont see it in the graph
My main issue now is that the Dates on the X axes Appear as the following "13 Feb 18", "14", "15" - why is the full date not shown?
For the suspicious category most likely is because it is constant, or just for a day. Let me check for the date
0
categories = df.Category.unique()   #to find unique categories
f,plots = plt.subplots(1,categories.len())   #generate subplot grid (1xNo. of unique categories)
for i, category in enumerate(categories):
    temp = df['Category'==category]   #get data for each category
    plots[i].scatter(temp['Date'],temp['Count'])   #plot for that category
plt.show()   #show final generated plot

Above is python code, there might be some syntax error but it will give you an idea how to solve your problem and how to code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.