'How to create two DIFFERENT interactive graphs (bar and line) with same data in one CELL using python?
Here's my sample code. However, it only shows a bar graph (user interactive) per cell. I want to add a USER INTERACTIVE LINE GRAPH of a 7-day moving average of the data. How?
df_cyp = df_exte[df_exte['location']=='Cyprus']
fig = px.bar(df_cyp, x='date', y='total_cases', template='plotly_dark',
color_discrete_sequence = px.colors.qualitative.G10,
labels=dict(date='April 2020 - October 2021', total_cases='Total Confirmed Cases'))
fig.update_layout(font_family='Rockwell')
fig.update_layout(title_text='CYPRUS TOTAL CASES', title_x=0.5)
fig['layout']['title']['font']= dict(size=25)
fig.show()
Solution 1:[1]
- simple case of adding a line to your existing bar chart. Line has been calculated using pandas as a moving average
- appears your data is COVID OWID data, so have used this
import pandas as pd
import io, requests
import plotly.express as px
df_exte = pd.read_csv(
io.StringIO(
requests.get(
"https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv"
).text
)
)
df_exte["date"] = pd.to_datetime(df_exte["date"])
df_exte[df_exte["location"] == "Cyprus"]
df_cyp = df_exte[df_exte["location"] == "Cyprus"]
fig = px.bar(
df_cyp,
x="date",
y="total_cases",
template="plotly_dark",
color_discrete_sequence=px.colors.qualitative.G10,
labels=dict(date="April 2020 - October 2021", total_cases="Total Confirmed Cases"),
)
fig.update_layout(font_family="Rockwell")
fig.update_layout(title_text="CYPRUS TOTAL CASES", title_x=0.5)
fig["layout"]["title"]["font"] = dict(size=25)
fig.add_traces(
px.line(
df_cyp.loc[:, ["date", "total_cases"]].rolling(window=7, on="date").mean(),
x="date",
y="total_cases",
).data
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Raymond |

