Python - Plotly:如何在矩形中心设置文本注释?
Python - Plotly: How to set the text annotation at the centre of a rectangle?
我在 plotly.express
箱形图中创建了阴影区域,我需要在它的顶部中心填充文本注释。
该段代码如下:
fig = px.box(data_frame = df,
x = 'N_ppm', y = 'N_dose',
color = 'pH_characterisation',
points = False,
facet_row = 'soil_text_3',
facet_col = 'org_mat_characterisations',
category_orders = {
'soil_text_3' : ['E', 'M', 'B']
}
)
fig.add_vrect(x0 = point_E,
x1 = top,
annotation_text="E",
annotation_position="top left",
fillcolor="green",
opacity=0.2,
line_width=0)
它会生成如下图:
如图所示,字母 'E' 位于左上角,而我需要它位于绿色矩形的顶部中心 space。我无法弄清楚我有哪些允许的选项,但 'top center' 不是其中之一,而 'top' 与 'top left'.
相同
你有什么解决办法吗?
- 有合成数据框可以运行你的代码,加上定义
point_E
和top
- 如果您检查已创建的布局,您将看到
add_vrect()
已创建包含文本 E[=33= 的 annotations ]
- 这些可以通过列表理解来修改,以更新此注释的 x 位置
完整代码
import pandas as pd
import numpy as np
import plotly.express as px
n = 3000
df = pd.DataFrame(
{
"N_ppm": np.random.randint(1, 10, n),
"N_dose": np.random.randint(1, 50, n),
"pH_characterisation": np.random.choice(list("ABC"), n),
"soil_text_3": np.random.choice(list("EMB"), n),
"org_mat_characterisations": np.random.choice(list("XYZ"), n),
}
)
point_E = 3
top = 7
fig = px.box(
data_frame=df,
x="N_ppm",
y="N_dose",
color="pH_characterisation",
points=False,
facet_row="soil_text_3",
facet_col="org_mat_characterisations",
category_orders={"soil_text_3": ["E", "M", "B"]},
)
fig.add_vrect(
x0=point_E,
x1=top,
annotation_text="E",
annotation_position="top left",
fillcolor="green",
opacity=0.2,
line_width=0,
)
fig.update_layout(
annotations=[
a if a["text"] != "E" else {**a, **{"x": ((top - point_E) / 2) + point_E}}
for a in fig.to_dict()["layout"]["annotations"]
]
)
输出
E 已输入。
我在 plotly.express
箱形图中创建了阴影区域,我需要在它的顶部中心填充文本注释。
该段代码如下:
fig = px.box(data_frame = df,
x = 'N_ppm', y = 'N_dose',
color = 'pH_characterisation',
points = False,
facet_row = 'soil_text_3',
facet_col = 'org_mat_characterisations',
category_orders = {
'soil_text_3' : ['E', 'M', 'B']
}
)
fig.add_vrect(x0 = point_E,
x1 = top,
annotation_text="E",
annotation_position="top left",
fillcolor="green",
opacity=0.2,
line_width=0)
它会生成如下图:
如图所示,字母 'E' 位于左上角,而我需要它位于绿色矩形的顶部中心 space。我无法弄清楚我有哪些允许的选项,但 'top center' 不是其中之一,而 'top' 与 'top left'.
相同你有什么解决办法吗?
- 有合成数据框可以运行你的代码,加上定义
point_E
和top
- 如果您检查已创建的布局,您将看到
add_vrect()
已创建包含文本 E[=33= 的 annotations ] - 这些可以通过列表理解来修改,以更新此注释的 x 位置
完整代码
import pandas as pd
import numpy as np
import plotly.express as px
n = 3000
df = pd.DataFrame(
{
"N_ppm": np.random.randint(1, 10, n),
"N_dose": np.random.randint(1, 50, n),
"pH_characterisation": np.random.choice(list("ABC"), n),
"soil_text_3": np.random.choice(list("EMB"), n),
"org_mat_characterisations": np.random.choice(list("XYZ"), n),
}
)
point_E = 3
top = 7
fig = px.box(
data_frame=df,
x="N_ppm",
y="N_dose",
color="pH_characterisation",
points=False,
facet_row="soil_text_3",
facet_col="org_mat_characterisations",
category_orders={"soil_text_3": ["E", "M", "B"]},
)
fig.add_vrect(
x0=point_E,
x1=top,
annotation_text="E",
annotation_position="top left",
fillcolor="green",
opacity=0.2,
line_width=0,
)
fig.update_layout(
annotations=[
a if a["text"] != "E" else {**a, **{"x": ((top - point_E) / 2) + point_E}}
for a in fig.to_dict()["layout"]["annotations"]
]
)
输出
E 已输入。