Plotly: difference between fig.update_layout({'yaxis': dict(matches=None)}) and fig.update_yaxes(matches=None)

huangapple go评论89阅读模式
英文:

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:

  1. import plotly.express as px
  2. import plotly
  3. print("plotly version: " , plotly.__version__)
  4. # Sample data
  5. data = {
  6. 'Variable': ['A', 'A', 'B', 'B', 'C', 'C'],
  7. 'Value': [1, 2, 3, 4, 5, 6]
  8. }
  9. # Create box plot
  10. fig = px.box(data, y='Value', facet_row='Variable')
  11. fig.update_layout(height=400, width=400)
  12. fig.update_layout({'yaxis': dict(matches=None)})
  13. fig.show()
  14. fig.update_yaxes(matches=None)
  15. fig.show()

OUT:

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:

  1. import plotly.express as px
  2. import plotly
  3. print("plotly version: " , plotly.__version__)
  4. # Sample data
  5. data = {
  6. 'Variable': ['A', 'A', 'B', 'B', 'C', 'C'],
  7. 'Value': [1, 2, 3, 4, 5, 6]
  8. }
  9. # Create box plot
  10. fig = px.box(data, y='Value', facet_row='Variable')
  11. fig.update_layout(height=400, width =400)
  12. fig.update_layout({'yaxis': dict(matches=None)})
  13. fig.show()
  14. fig.update_yaxes(matches=None)
  15. fig.show()

OUT:

Plotly: difference between fig.update_layout({'yaxis': dict(matches=None)}) and fig.update_yaxes(matches=None)

答案1

得分: 1

这是因为当你使用facet_row参数时,会生成多个y轴。你可以像这样分别设置它们:

  1. fig.update_layout({'yaxis': dict(matches=None)})
  2. fig.update_layout({'yaxis2': dict(matches=None)})
  3. fig.update_layout({'yaxis3': dict(matches=None)})

或者以更动态的方式:

  1. ## 检索所有y轴名称: ['yaxis', 'yaxis2', 'yaxis3']
  2. yaxis_names = ['yaxis'] + [axis_name for axis_name in fig.layout._subplotid_props if 'yaxis' in axis_name]
  3. yaxis_layout_dict = {yaxis_name:dict(matches=None) for yaxis_name in yaxis_names}
  4. 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:

  1. fig.update_layout({'yaxis': dict(matches=None)})
  2. fig.update_layout({'yaxis2': dict(matches=None)})
  3. fig.update_layout({'yaxis3': dict(matches=None)})

Or in a more dynamic way:

  1. ## retrieve all yaxis names: ['yaxis', 'yaxis2', 'yaxis3']
  2. yaxis_names = ['yaxis'] + [axis_name for axis_name in fig.layout._subplotid_props if 'yaxis' in axis_name]
  3. yaxis_layout_dict = {yaxis_name:dict(matches=None) for yaxis_name in yaxis_names}
  4. fig.update_layout(yaxis_layout_dict)

huangapple
  • 本文由 发表于 2023年5月25日 21:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定