如何使用 python 在一个 CELL 中创建两个具有相同数据的不同交互式图形(条形图和线形图)?
How to create two DIFFERENT interactive graphs (bar and line) with same data in one CELL using python?
这是我的示例代码。但是,它只显示每个单元格的条形图(用户交互)。我想添加数据的 7 天移动平均值的用户交互线图。怎么样?
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()
在plotly中,我明白了我想使用subplot设置,在左边画一个条形图,在右边画一个折线图。由于没有给出数据,我以股票数据的成交量和收盘价为例来创建代码。
import yfinance as yf
df = yf.download("AAPL", start="2021-04-01", end="2021-10-01")
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Bar(x=df.index,
y=df['Volume'],
name='Volume'),
secondary_y=False)
fig.add_trace(go.Scatter(x=df.index,
y=df['Close'],
mode='lines',
name='Close',
line=dict(color='green')
),secondary_y=True)
fig.update_layout(font_family='Rockwell',
title_text='CYPRUS TOTAL CASES',
title_x=0.5,
title_font=dict(size=25),
template='plotly_dark',
yaxis_title='Total Confirmed Cases',
xaxis_title='April 2020 - October 2021',
)
fig.show()
- 向现有条形图添加线条的简单案例。已使用 pandas 作为移动平均线
计算线
- 看来您的数据是 COVID OWID 数据,所以使用了这个
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
)
这是我的示例代码。但是,它只显示每个单元格的条形图(用户交互)。我想添加数据的 7 天移动平均值的用户交互线图。怎么样?
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()
在plotly中,我明白了我想使用subplot设置,在左边画一个条形图,在右边画一个折线图。由于没有给出数据,我以股票数据的成交量和收盘价为例来创建代码。
import yfinance as yf
df = yf.download("AAPL", start="2021-04-01", end="2021-10-01")
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Bar(x=df.index,
y=df['Volume'],
name='Volume'),
secondary_y=False)
fig.add_trace(go.Scatter(x=df.index,
y=df['Close'],
mode='lines',
name='Close',
line=dict(color='green')
),secondary_y=True)
fig.update_layout(font_family='Rockwell',
title_text='CYPRUS TOTAL CASES',
title_x=0.5,
title_font=dict(size=25),
template='plotly_dark',
yaxis_title='Total Confirmed Cases',
xaxis_title='April 2020 - October 2021',
)
fig.show()
- 向现有条形图添加线条的简单案例。已使用 pandas 作为移动平均线 计算线
- 看来您的数据是 COVID OWID 数据,所以使用了这个
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
)