如何在plotly dash中将垂直线添加到直方图?

2024-09-30 01:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个graph组件,它在回调时被触发。下面是我从回调函数中绘制直方图的代码

xtup = np.random.lognormal(90, 3, size=1000)
xtup = list(map('{:.0f}'.format,xtup[0]))
xarr = np.asarray(xtup).astype(np.float)

data = [go.Histogram(
                     x = xarr,
                     nbinsx=50,
                     marker=dict(color="rgb(105,105,105)",
                                 opacity=0.5
                                ),

       )]

layout = {
            "xaxis": {'title': 'Values',
                      'tickformat': '${:,.0f}'
                      #'range': [xarr.min(), xarr.max()]
                     },
            "title": "Distribution",
            "width": 650,
            "height": 400,
            "autosize": True
}

return{'data':data, 'layout':layout}

我想在分布的平均值处加一条垂直虚线


Tags: 函数代码datasizetitlenp绘制组件
1条回答
网友
1楼 · 发布于 2024-09-30 01:28:50

您的代码片段并不容易复制,但在这个^{}示例的基础上,您可以包括:

fig.add_vline(x=np.median(df.total_bill), line_dash = 'dash', line_color = 'firebrick')

并获得:

enter image description here

请注意^{}需要最新的plotly版本才能正常工作

完整代码:

import plotly.express as px
import numpy as np
df = px.data.tips()
fig = px.histogram(df, x="total_bill")
fig.add_vline(x=np.median(df.total_bill), line_dash = 'dash', line_color = 'firebrick')
fig.show()

相关问题 更多 >

    热门问题