How to plot cumulative data using plotly in python?

3.2k Views Asked by At

I am using Google colab to generate graphs and charts using plotly in python. I have 6,97,000 rows of data stored in a csv file that I am analyzing. I am using the following code to generate a bar chart and it works perfectly.

fig = px.bar(df, x='IP', y="Epid_ID")
fig.update_traces(marker=dict(line=dict(width=3,color='blue')))
fig.show()

Now, I want a chart showing cumulative data. Following is an example of my dataset.

IP             Epid_ID
05/08/2021     COV-NEP-PR4-LAM-21-01936
05/08/2021     COV-NEP-PR4-LAM-21-01937
06/08/2021     COV-NEP-PR4-LAM-21-01938
06/08/2021     COV-NEP-PR4-LAM-21-01939
07/08/2021     COV-NEP-PR4-LAM-21-01940

My expected output is a bar chart showing cumulative data. Current output:

enter image description here

Expected Output

enter image description here

I tried to use cumsum using the following link. https://www.codegrepper.com/code-examples/python/cumulative+chart+python+plotly

And tried to keep the Date variable as x using the following codes.

 x = df['IP']
 y = df['Epid_ID']
 cumsum = np.cumsum(x)

However, my runtime crashes when I use this code. Please help!

2

There are 2 best solutions below

0
Tashi On BEST ANSWER

Building a histogram will provide you the expected output cause it distributes data in ranges.

Try using this

import plotly.express as px
import plotly.graph_objects as go 
    
df = px.data.iris() 
    
fig = go.Figure(data=[go.Histogram(y=df['sepal_width'], cumulative_enabled=True)]) 
fig.show()
1
JAdel On

So I interpret that you want an ascending sorted output by count? Did you try to sort the DataFrame or SubDataFrame with df['Epid_ID'].sort("Epid_ID",ascending=False). You also can try to aggregate the DataFrame before with .count().

df.groupBy("salutation").count().sort("count",ascending=False).show()
+------------+------+
|  salutation| count|
+------------+------+
|not reported|   255|
|     Company|   321|
|      Family|  1467|
|          Mr| 12012|
|         Mrs|382567|
+------------+------+