如何在 plotly 上的分组条形图上指定 x 坐标?
How to specify the x coordinate on a grouped bar chart on plotly?
我用 python 绘制了一个条形图,我想在特定的条形图上做一个标记,例如不吸烟的女性。
有谁知道如何指定这个?
我从 plotly 文档中举了一个例子,如果我尝试放置标记,它只会占据主要类别的中心。
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="sex", y="total_bill",
color='smoker', barmode='group',
height=400)
#trying to set the marker
fig.add_trace(
go.Scatter(x=["Female"],
y=[1100]
))
fig.show()
- 受此启发:https://community.plotly.com/t/grouped-bar-charts-with-corresponding-line-chart/19562/4
- 使用xaxis2,算出位置,硬编码了,但
0.15
与bargoup中的轨迹数和x值 有关
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
df = px.data.tips()
fig = px.histogram(
df, x="sex", y="total_bill", color="smoker", barmode="group", height=400
)
# trying to set the marker
fig.add_trace(
go.Scatter(
x=[0.15],
y=[1100],
customdata=[["No", "Female"]],
xaxis="x2",
hovertemplate="smoker=%{customdata[0]}<br>sex=%{customdata[1]}<br>sum of total_bill=%{y}<extra></extra>",
)
)
fig.update_layout(xaxis2={"overlaying": "x", "range": [0, 1], "showticklabels": False})
fig
我用 python 绘制了一个条形图,我想在特定的条形图上做一个标记,例如不吸烟的女性。 有谁知道如何指定这个?
我从 plotly 文档中举了一个例子,如果我尝试放置标记,它只会占据主要类别的中心。
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="sex", y="total_bill",
color='smoker', barmode='group',
height=400)
#trying to set the marker
fig.add_trace(
go.Scatter(x=["Female"],
y=[1100]
))
fig.show()
- 受此启发:https://community.plotly.com/t/grouped-bar-charts-with-corresponding-line-chart/19562/4
- 使用xaxis2,算出位置,硬编码了,但
0.15
与bargoup中的轨迹数和x值 有关
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
df = px.data.tips()
fig = px.histogram(
df, x="sex", y="total_bill", color="smoker", barmode="group", height=400
)
# trying to set the marker
fig.add_trace(
go.Scatter(
x=[0.15],
y=[1100],
customdata=[["No", "Female"]],
xaxis="x2",
hovertemplate="smoker=%{customdata[0]}<br>sex=%{customdata[1]}<br>sum of total_bill=%{y}<extra></extra>",
)
)
fig.update_layout(xaxis2={"overlaying": "x", "range": [0, 1], "showticklabels": False})
fig