Plotly:更新所有 yaxes 字体并使用预设布局给出单独的比例
Plotly: Update all yaxes fonts and give individual scale using preset layout
我有一个数据集 df = ['Group', 'subgroup', 'x', 'y'],我想
- 绘制散点图 x 与 y-(完成)
- 颜色 = 子组(完成)
- facet_rows = 组(完成)
现在我想用我自己的布局更新剧情 (my_layout);但是我遇到了两个问题:
- 当我通过 fig.update_layout(my_layout) 时,它只更新一个 yaxis,而不是所有。
- 使用“匹配”:None,对于 yaxis,不会为 yaxis 产生 2 个单独的比例。
使用 fig.update_yaxes(matches=None)2,正确更新 yaxis,但是我想把它全部放在 my_layout 中,这样每次我都会生成一个新的plot 我可以用一个命令更新所有布局。
代码如下:
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({
"yaxis": {"matches": None, "titlefont": dict(family = 'Times new Roman',size = 30, color = 'black'), "tickfont": dict(family = 'Times new Roman',size = 30, color = 'black'),},
"xaxis": {"titlefont": dict(family = 'Times new Roman',size = 30, color = 'black'), "tickfont": dict(family = 'Times new Roman',size = 30, color = 'black'),},
})
#the df
df =pd.DataFrame({
"Group" : [1,1,1,1,2,2,2,2],
"subgroup": ["A","B","C","D","E","F","G","H"],
"x" : [1,2,3,4,1,2,3,4],
"y" : [2,4,6,8,20,40,60,80]
})
fig = px.scatter(df, x = "x", y= "y", color="subgroup", facet_row="Group", template = "simple_white")
fig.update_layout(my_layout)
#with this line commented in plots yaxis update correctly, hower I am hoping to put all layout components into one object i can call.
# fig.update_yaxes(matches=None)
fig.show()
plotly中的subplot设置按顺序赋值:xaxis,xaxis2,xaxis3,依此类推。 y-axis 是一样的。因此,需要在自己的布局中加入yaxis2。 fig.layout
允许您以字典格式可视化图形的结构。
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
#the df
df =pd.DataFrame({
"Group" : [1,1,1,1,2,2,2,2],
"subgroup": ["A","B","C","D","E","F","G","H"],
"x" : [1,2,3,4,1,2,3,4],
"y" : [2,4,6,8,20,40,60,80]
})
fig = px.scatter(df, x="x", y="y", color="subgroup", facet_row="Group", template="simple_white")
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({"yaxis": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"yaxis2": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"xaxis": {"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
})
fig.update_layout(my_layout)
fig.show()
编辑:要优化 y-axis 范围,我不能在 express 中做到这一点,所以唯一的方法是在 graph_objects 中对其进行子图。我正在重用在 express 中创建的数据并对其进行子绘图。我们决定用两种不同的方式来处理这个问题,这对 plotly 用户更有利。
fig = make_subplots(rows=2, cols=1, shared_xaxes=True)
fig_data = px.scatter(df, x="x", y="y", color="subgroup", facet_row="Group", template="simple_white")
for i in range(8):
if i <=3:
fig.add_trace(fig_data.data[i])
else:
fig.add_trace(fig_data.data[i])
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({"yaxis": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"yaxis2": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"xaxis2": {"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"template":'simple_white',
})
fig.update_layout( my_layout)
fig.layout.legend.tracegroupgap = 1
fig.show()
我有一个数据集 df = ['Group', 'subgroup', 'x', 'y'],我想
- 绘制散点图 x 与 y-(完成)
- 颜色 = 子组(完成)
- facet_rows = 组(完成)
现在我想用我自己的布局更新剧情 (my_layout);但是我遇到了两个问题:
- 当我通过 fig.update_layout(my_layout) 时,它只更新一个 yaxis,而不是所有。
- 使用“匹配”:None,对于 yaxis,不会为 yaxis 产生 2 个单独的比例。
使用 fig.update_yaxes(matches=None)2,正确更新 yaxis,但是我想把它全部放在 my_layout 中,这样每次我都会生成一个新的plot 我可以用一个命令更新所有布局。
代码如下:
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({
"yaxis": {"matches": None, "titlefont": dict(family = 'Times new Roman',size = 30, color = 'black'), "tickfont": dict(family = 'Times new Roman',size = 30, color = 'black'),},
"xaxis": {"titlefont": dict(family = 'Times new Roman',size = 30, color = 'black'), "tickfont": dict(family = 'Times new Roman',size = 30, color = 'black'),},
})
#the df
df =pd.DataFrame({
"Group" : [1,1,1,1,2,2,2,2],
"subgroup": ["A","B","C","D","E","F","G","H"],
"x" : [1,2,3,4,1,2,3,4],
"y" : [2,4,6,8,20,40,60,80]
})
fig = px.scatter(df, x = "x", y= "y", color="subgroup", facet_row="Group", template = "simple_white")
fig.update_layout(my_layout)
#with this line commented in plots yaxis update correctly, hower I am hoping to put all layout components into one object i can call.
# fig.update_yaxes(matches=None)
fig.show()
plotly中的subplot设置按顺序赋值:xaxis,xaxis2,xaxis3,依此类推。 y-axis 是一样的。因此,需要在自己的布局中加入yaxis2。 fig.layout
允许您以字典格式可视化图形的结构。
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
#the df
df =pd.DataFrame({
"Group" : [1,1,1,1,2,2,2,2],
"subgroup": ["A","B","C","D","E","F","G","H"],
"x" : [1,2,3,4,1,2,3,4],
"y" : [2,4,6,8,20,40,60,80]
})
fig = px.scatter(df, x="x", y="y", color="subgroup", facet_row="Group", template="simple_white")
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({"yaxis": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"yaxis2": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"xaxis": {"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
})
fig.update_layout(my_layout)
fig.show()
编辑:要优化 y-axis 范围,我不能在 express 中做到这一点,所以唯一的方法是在 graph_objects 中对其进行子图。我正在重用在 express 中创建的数据并对其进行子绘图。我们决定用两种不同的方式来处理这个问题,这对 plotly 用户更有利。
fig = make_subplots(rows=2, cols=1, shared_xaxes=True)
fig_data = px.scatter(df, x="x", y="y", color="subgroup", facet_row="Group", template="simple_white")
for i in range(8):
if i <=3:
fig.add_trace(fig_data.data[i])
else:
fig.add_trace(fig_data.data[i])
#my layoyt, shoudl update all plots yaxis, and set indivuual scales for yaxis
my_layout = go.Layout({"yaxis": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"yaxis2": {"matches": None,
"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"xaxis2": {"titlefont": dict(family='Times new Roman',size=30, color='black'),
"tickfont": dict(family='Times new Roman',size=30, color='black'),},
"template":'simple_white',
})
fig.update_layout( my_layout)
fig.layout.legend.tracegroupgap = 1
fig.show()