Reputation: 23
I am trying to plot a multi line graph from pandas in python. I need three lines (positive, negative, neutral) and their number of occurrences during time. On the x-axis I would have the time, on the y- axis I would have the number of occurrences and I would have three lines in one graph. If there is no occurrence then it would be automatically zero (for example in th etable there is no occurrence for "negative" so in the graph the point would be zero). I am attaching the table that I would need to turn into graph.
d = datapandas.groupby(["date","classification"]).size()
The most left column is the number of occurrence of the classification during the date. I could not find any easy way to plot this kind of graph.
Upvotes: 1
Views: 536
Reputation: 120559
You have just to unstack
the classification
index and plot the graph:
d.unstack('classification').fillna(0).plot()
Note: you can avoid groupby
by using value_counts
:
d = datapandas.value_counts(['date', 'classification'])
Upvotes: 1
Reputation: 6367
You can try to use pivot
to rearrange your data and then call plot()
this is by default kind='line'
. To fill the missing values you can call fillna(0)
.
If you have a MultiIndex call reset_index('classification')
first.
df.pivot(columns='classification', values='value').fillna(0).plot()
Here is a complete minimal example:
from io import StringIO
import pandas as pd
t = '''date classification value
2021-12-24 neutral 3
2021-12-24 positiv 2
2021-12-25 neutral 1
2021-12-25 positiv 3
2021-12-26 neutral 1
2021-12-26 negative 2
'''
df = pd.read_csv(StringIO(t), sep='\s', parse_dates=[0], index_col=0)
df.pivot(columns='classification', values='value').fillna(0).plot(kind='line')
Upvotes: 0