我用python绘出了一个条形图,我想在一个特定的条形图上画一个标记,比如不吸烟的女性.

我从plotly文档中取了一个例子,如果我try 放置标记,它只会占据主类别的中心.

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()

enter image description here

推荐答案

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

enter image description here

Python相关问答推荐

Python在通过Inbox调用时给出不同的响应

是否有方法将现有的X-Y图转换为X-Y-Y1图(以重新填充)?

键盘.任务组

如果AST请求默认受csref保护,那么在Django中使用@ system_decorator(csref_protect)的目的是什么?

在Arrow上迭代的快速方法.Julia中包含3000万行和25列的表

在编写要Excel的数据透视框架时修复标题行

Chatgpt API不断返回错误:404未能从API获取响应

将numpy数组存储在原始二进制文件中

使用FASTCGI在IIS上运行Django频道

Matlab中是否有Python的f-字符串等效物

如何删除索引过go 的lexsort深度可能会影响性能?' &>

海运图:调整行和列标签

基于字符串匹配条件合并两个帧

如何根据一列的值有条件地 Select 前N组?

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

在Django admin中自动完成相关字段筛选

无法连接到Keycloat服务器

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

Odoo16:模板中使用的docs变量在哪里定义?