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

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

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:

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:

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

答案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)

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:

确定