XzMn
January 13, 2020, 6:18pm
1
Hi,
I want to create a time graph but the problem is that I have a csv with the same dates, but I don’t know how to graph it. I’ll keep the csv short:
Date,Quantity
2019-23-05,1
2019-23-05,4
2019-23-05,2
2019-23-05,4
2019-23-05,4
2019-23-05,4
2019-23-05,4
2019-24-05,7
2019-24-05,1
2019-23-07,1
2020-11-01,3
And my code(Updated ):
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("Publications1.csv")
dfg = df.groupby(['Date']).sum()
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dfg.index,
y=dfg['Quantity'],
name="Example graph",
line_color='deepskyblue',
opacity=0.8))
fig.update_layout(xaxis_range=['2019-03-01','2020-01-31'],
title_text="Data recolected")
fig.show()
I want the amount to be shown on the graph with the sum of each month’s values. For example, 2019-23-05 has 23 quantities and this should be reflected in the graph as one point.
Thank you very much.
Hi @XzMn , welcome to the forum! If you’re using px.bar
from plotly.express
, you will get a graph with stacked bars so that the height of the bar will be the total for the date (fig = px.bar(df, x='Date', y='Quantity')
). You can also plot a go.Histogram
with histfunc='sum'
. If you want scatter points, then you have to transform first the dataframe to compute the sum, using a groupby operation (df.groupby('Date').sum()
), see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html and https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html
empet
January 13, 2020, 6:43pm
3
Hi @XzMn ,
To define a trace of type scatter, as you want, just group your data in the Dataframe, by date, i.e. define a new dataframe as follows:
dfg = df.groupby(['Date']).sum()
dfg has date as index and the column ‘Quantity’. Then define the trace by:
fig.add_trace(go.Scatter(
x=dfg.index,
y=dfg['Quantity'],
name="Example graph",
line_color='deepskyblue',
opacity=0.8))
1 Like
XzMn
January 13, 2020, 6:49pm
4
XzMn:
Date,Quantity 2019-23-05,1 2019-23-05,4 2019-23-05,2 2019-23-05,4 2019-23-05,4 2019-23-05,4 2019-23-05,4 2019-24-05,7 2019-24-05,1 2019-23-07,1 2020-11-01,3
Hi, thanks for you response. I put your code lines in my code but the graph is not displayed
XzMn
January 13, 2020, 6:49pm
5
Hi, thanks for you response. I put your code lines in my code but the graph is not displayed
Can you share the updated code?
XzMn
January 13, 2020, 6:54pm
7
Yes, I already update the code.
empet
January 13, 2020, 7:07pm
8
@XzMn
Here is the complete code:
import numpy as np
import plotly.graph_objs as go
import pandas as pd
from datetime import datetime
d ={'Date': [datetime(2019, 5, 23)]*7+ [datetime(2019, 5,24)]*2+ [datetime(2019, 7, 1), datetime(2020, 1, 11)],
'Quantity' : [1, 4, 2, 4, 4, 4, 4,7, 1, 1, 3]}
df = pd.DataFrame(d)
dfg = df.groupby(['Date']).sum()
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dfg.index,
y=dfg['Quantity'],
name="Example graph",
line_color='deepskyblue',
opacity=0.8))
The dataframe dfg (after grouping by date):
Quantity
Date
2019-05-23 23
2019-05-24 8
2019-07-01 1
2020-01-11 3
XzMn
January 13, 2020, 7:12pm
9
Thanks! and it works! but how would it be with the CSV? Because as you did, it’s without reading the CSV data. I’d appreciate your cooperation!
Use df['Date'] = pd.to_datetime(df['Date'], format="%Y-%d-%m")
to convert to a format understood by plotly.
XzMn
January 13, 2020, 7:22pm
11
There, that was it! I thank you very much. That worked well.