英文:
Plotly: difference between fig.update_layout({'yaxis': dict(matches=None)}) and fig.update_yaxes(matches=None)
问题
I am trying to work out the difference between:
fig.update_layout({'yaxis': dict(matches=None)})
fig.update_yaxes(matches=None)
I thought they were the same, but fig.update_layout({'yaxis': dict(matches=None)})
doesn't change the yaxis as expected.
Here is the example code:
import plotly.express as px
import plotly
print("plotly version: " , plotly.__version__)
# Sample data
data = {
'Variable': ['A', 'A', 'B', 'B', 'C', 'C'],
'Value': [1, 2, 3, 4, 5, 6]
}
# Create box plot
fig = px.box(data, y='Value', facet_row='Variable')
fig.update_layout(height=400, width=400)
fig.update_layout({'yaxis': dict(matches=None)})
fig.show()
fig.update_yaxes(matches=None)
fig.show()
OUT:
英文:
I am trying to work out the difference between:
fig.update_layout({'yaxis': dict(matches=None)})
fig.update_yaxes(matches=None)
I thought they were the same, but fig.update_layout({'yaxis': dict(matches=None)})
doesn't change the yaxis as expected.
Here is the example code:
import plotly.express as px
import plotly
print("plotly version: " , plotly.__version__)
# Sample data
data = {
'Variable': ['A', 'A', 'B', 'B', 'C', 'C'],
'Value': [1, 2, 3, 4, 5, 6]
}
# Create box plot
fig = px.box(data, y='Value', facet_row='Variable')
fig.update_layout(height=400, width =400)
fig.update_layout({'yaxis': dict(matches=None)})
fig.show()
fig.update_yaxes(matches=None)
fig.show()
OUT:
答案1
得分: 1
这是因为当你使用facet_row
参数时,会生成多个y轴。你可以像这样分别设置它们:
fig.update_layout({'yaxis': dict(matches=None)})
fig.update_layout({'yaxis2': dict(matches=None)})
fig.update_layout({'yaxis3': dict(matches=None)})
或者以更动态的方式:
## 检索所有y轴名称: ['yaxis', 'yaxis2', 'yaxis3']
yaxis_names = ['yaxis'] + [axis_name for axis_name in fig.layout._subplotid_props if 'yaxis' in axis_name]
yaxis_layout_dict = {yaxis_name:dict(matches=None) for yaxis_name in yaxis_names}
fig.update_layout(yaxis_layout_dict)
英文:
This is happening because when you use the facet_row
argument, you will generate multiple yaxes. You can set them individually like this:
fig.update_layout({'yaxis': dict(matches=None)})
fig.update_layout({'yaxis2': dict(matches=None)})
fig.update_layout({'yaxis3': dict(matches=None)})
Or in a more dynamic way:
## retrieve all yaxis names: ['yaxis', 'yaxis2', 'yaxis3']
yaxis_names = ['yaxis'] + [axis_name for axis_name in fig.layout._subplotid_props if 'yaxis' in axis_name]
yaxis_layout_dict = {yaxis_name:dict(matches=None) for yaxis_name in yaxis_names}
fig.update_layout(yaxis_layout_dict)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论