Plotly中通过下拉选择修改子图Y轴标题文本
问题描述
我制作了一个包含3个子图和1个下拉菜单的Plotly图表,希望通过选择下拉选项修改子图的Y轴标题文本,但yaxis.title或yaxis2.title设置不生效。注意:不使用Dash。请问是否可以仅修改第3个子图的Y轴标题文本,而不改动第1和第2个子图?
原代码如下:
import plotly.graph_objs as go from plotly import subplots trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers") trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers") trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers") fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True,horizontal_spacing=0.5) fig.add_trace(trace0, 1, 1) fig.add_trace(trace1, 1, 1) fig.add_trace(trace2, 1, 1) fig.add_trace(trace0, 2, 1) fig.add_trace(trace0, 3, 1) update_menus = [go.layout.Updatemenu( active=0, buttons=list( [dict(label = 'All', method = 'relayout', args = [{'visible': [True, True, True,True,True]}, {'title': 'all', 'showlegend':True}, {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]), dict(label = 'First', method = 'relayout', args = [{'visible': [True, False, False,True,True]}, # the index of True aligns with the indices of plot traces {'title': 'first', 'showlegend':True}, {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]), dict(label = 'Second', method = 'relayout', args = [{'visible': [False, True, False,True,True]}, {'title': 'second', 'showlegend':True}, {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]), dict(label = 'Third', method = 'relayout', args = [{'visible': [False, False, True, False,False]}, {'title': 'third', 'showlegend':True}, {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]), ]) ) ] fig.show()
解决方案
可以实现仅修改第3个子图的Y轴标题,核心问题是当前relayout的参数格式错误——args里的多个字典需要合并成一个,Plotly的relayout方法只会处理第一个字典参数,后面的字典会被忽略。
另外,若只想修改第3个子图的Y轴标题,无需每次重复设置前两个子图的标题,仅在对应按钮的参数里指定yaxis3.title即可,前两个子图的标题会保持原有设置。
修改后的完整代码如下:
import plotly.graph_objs as go from plotly import subplots trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers") trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers") trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers") # 初始化子图并设置默认Y轴标题 fig = subplots.make_subplots(rows=3, cols=1, shared_xaxes=True, horizontal_spacing=0.5) fig.add_trace(trace0, 1, 1) fig.add_trace(trace1, 1, 1) fig.add_trace(trace2, 1, 1) fig.add_trace(trace0, 2, 1) fig.add_trace(trace0, 3, 1) # 统一设置初始Y轴标题 fig.update_layout( yaxis_title="fgv", yaxis2_title="ram", yaxis3_title="gen" ) update_menus = [go.layout.Updatemenu( active=0, buttons=[ dict( label='All', method='relayout', args=[{ 'visible': [True, True, True, True, True], 'title': 'all', 'showlegend': True }] ), dict( label='First', method='relayout', args=[{ 'visible': [True, False, False, True, True], 'title': 'first', 'showlegend': True }] ), dict( label='Second', method='relayout', args=[{ 'visible': [False, True, False, True, True], 'title': 'second', 'showlegend': True }] ), dict( label='Third', method='relayout', args=[{ 'visible': [False, False, True, False, False], 'title': 'third', 'showlegend': True, # 仅修改第3个子图的Y轴标题 'yaxis3.title': '自定义标题' }] ) ] )] fig.update_layout(update_menus=update_menus) fig.show()
关键修改点:
- 将
args中的多个字典合并为一个,确保relayout能读取到所有参数 - 仅在需要修改的按钮(如"Third")中指定
yaxis3.title,其他按钮无需额外设置Y轴标题,前两个子图会维持初始设置 - 初始化时通过
update_layout统一设置默认Y轴标题,减少重复代码
内容的提问来源于stack exchange,提问作者OneTouchForHeight
相关产品推荐
相关产品推荐

