[英]Dash/Plotly: How can I plot two columns on the y-axis with the same x axis?
[英]Plotly: Putting y-axis two plots in the same range in Python
我有兩個並排的箱形圖,但我想讓它們具有相同的 y 軸范圍(在這種情況下為 150 - 400)。 目前,右側的 plot 與左側的 y 軸范圍不同。 我已經嘗試使用fig.update_layout(yaxis_range=[140,410]) ,但我在這里沒有任何成功。
這是代碼:
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from plotly.subplots import make_subplots
df2 = pd.DataFrame()
np.random.seed(42)
df2['date'] = pd.date_range('2021-01-01', '2021-12-20', freq='D')
df2['month'] = df2['date'].dt.month
df2['spot_rate'] = np.random.randint(low=150, high=400, size=len(df2.index))
df4 = df2[df2['month'] == 12].tail(15)
fig = go.Figure()
fig = make_subplots(rows=1, cols=2, column_widths=[0.9, 0.1])
trendline = df2.groupby(['month']).spot_rate.median()
x1,y1 = list(trendline.index), trendline.values
stds = df2.groupby(['month']).spot_rate.std().values
n_vals = df2.groupby(['month']).date.count().values
## construct a 0.95 CI using mean ± z*std/sqrt(n)
y1_upper = list(y1 + 1.96*stds/np.sqrt(n_vals))
y1_lower = list(y1 - 1.96*stds/np.sqrt(n_vals))
y1_lower = y1_lower[::-1]
x1_rev = x1[::-1]
fig.add_trace(go.Scatter(x=x1, y=y1, line_shape="spline", line_color="blue", name="trendline"))
fig.add_trace(go.Scatter(
x=x1+x1_rev,
y=y1_upper+y1_lower,
line_shape="spline",
fill='toself',
fillcolor='rgba(255,192,203,0.5)',
line_color='rgba(255,255,255,0)',
showlegend=False,
name="0.95 CI",
))
fig.add_trace(go.Box(y = df2['spot_rate'],
x = df2['month'],
marker_color = '#6AF954',
name = 'Monthly'),
row=1, col=1)
fig.add_trace(go.Box(y = df4['spot_rate'],
x = df4['month'],
marker_color = "orange",
name = "Past 15 days"),
row=1, col=2)
fig.update_layout(paper_bgcolor="black",
plot_bgcolor="black",#template="plotly_dark",
font_color="white",
title="Spot Rates by Time",
xaxis_title="Month",
yaxis_title="Spot Rate",
yaxis_range=[140,410])
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.