[英]Plotly - how to display y values when hovering on two subplots sharing x-axis
[英]Python Plotly: Sharing x-axis and making subplots by group
我试图让两个子图共享 x 轴,并按Type
将它们分开,如下所示:
供参考
A_df
和B_df
,让您了解我想要做什么,但欢迎您使用final_df
!这是可重现的代码:
rng = pd.date_range('2022-04-09', periods=20, freq='D')
first_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
first_df['Type'] = 'A'
second_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
second_df['Type'] = 'B'
final_df = pd.concat([first_df,second_df]).sort_values(by = 'Date')
final_df['Is_Weekend'] = np.where((final_df['Date'].dt.weekday == 5), 1, 0 )
A_df = final_df[final_df['Type']=='A']
B_df = final_df[final_df['Type']=='B']
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=A_df['Date'], y=A_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)
fig.update_xaxes(showgrid=False)
fig.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = 'rgba(0,0,0,0)')
fig.add_trace(go.Scatter(x=A_df['Date'],
y = A_df['Val'],
line_color = 'orange',
mode = 'lines+markers',
showlegend = False),
secondary_y = False)
fig.show()
fig2 = make_subplots(specs=[[{"secondary_y": True}]])
fig2.add_trace(go.Scatter(x=B_df['Date'], y=B_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)
fig2.update_xaxes(showgrid=False)
fig2.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = 'rgba(0,0,0,0)')
fig2.add_trace(go.Scatter(x=B_df['Date'],
y = B_df['Val'],
line_color = 'blue',
mode = 'lines+markers',
showlegend = False),
secondary_y = False)
fig2.show()
编辑:
如何将顺序更改为图例?
如果要将两个图形组合成一个带有子图和共享 xaxis 的图形,您可以通过以下方式定义图形:
fig = make_subplots(rows=2, cols=1,
shared_xaxes=True,
specs=[[{"secondary_y": True}],[{"secondary_y": True}]])
然后,当您添加跟踪和更新布局时,您可以使用适当的row
和col
参数。
例如:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
rng = pd.date_range('2022-04-09', periods=20, freq='D')
np.random.seed(42)
first_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
first_df['Type'] = 'A'
second_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
second_df['Type'] = 'B'
final_df = pd.concat([first_df,second_df]).sort_values(by = 'Date')
final_df['Is_Weekend'] = np.where((final_df['Date'].dt.weekday == 5), 1, 0 )
A_df = final_df[final_df['Type']=='A']
B_df = final_df[final_df['Type']=='B']
fig = make_subplots(rows=2, cols=1,
shared_xaxes=True,
specs=[[{"secondary_y": True}],[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=A_df['Date'], y=A_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)
fig.update_xaxes(showgrid=False, row=1, col=1)
fig.update_yaxes(range=[-0,0.1], showgrid=False, tickfont_color = 'rgba(0,0,0,0)', secondary_y=True, row=1, col=1)
fig.add_trace(go.Scatter(x=A_df['Date'],
y = A_df['Val'],
line_color = 'orange',
mode = 'lines+markers',
showlegend = False),
row=1, col=1,
secondary_y = False)
fig.add_trace(go.Scatter(x=B_df['Date'], y=B_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row=2, col=1, secondary_y=True)
fig.update_xaxes(showgrid=False, row=2, col=1)
fig.update_yaxes(range=[-0,0.1], showgrid=False, tickfont_color = 'rgba(0,0,0,0)', secondary_y=True, row=2, col=1)
fig.add_trace(go.Scatter(x=B_df['Date'],
y = B_df['Val'],
line_color = 'blue',
mode = 'lines+markers',
showlegend = False),
row=2, col=1,
secondary_y = False)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.