打印密度热图格式颜色比例和悬停文字

2024-10-01 04:47:59 发布

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

我正试图在一个标准化为“百分比”的绘图密度热图上设置色标的格式。颜色刻度和数字看起来正确,但刻度显示为0.015而不是1.5%;这对我来说很好,但最终图表需要呈现给那些不愿意接受的人

enter image description here

sims = sims.iloc[:,f:].stack().reset_index()
sims.columns = ['sim', 'period', 'return']

fig = px.density_heatmap(sims, x='period', y='return', nbinsx=nbinsx,
                         histnorm='percent', histfunc='avg',
                         labels={'period':'Sim Period',
                                 'return':'Annualised Return'},)

fig.update_traces(dict(colorscale='Tealrose',showscale=True,coloraxis=None),)
fig.update_layout(yaxis={'hoverformat':'.1%', 'tickformat':'.0%',},
                  xaxis={'hoverformat':':.0f', 'tickformat':':.0f',},
                  )

如果我正确理解了Plotly docs,histfunc应该是z轴的一个属性,但是当我查看生成的图形时,“z”中没有数据(我可能在这里做错了什么)

Histogram2d({
    'colorscale': [[0.0, 'rgb(0, 147, 146)'], [0.16666666666666666, 'rgb(114, 170,
                   161)'], [0.3333333333333333, 'rgb(177, 199, 179)'], [0.5,
                   'rgb(241, 234, 200)'], [0.6666666666666666, 'rgb(229, 185,
                   173)'], [0.8333333333333334, 'rgb(217, 137, 148)'], [1.0,
                   'rgb(208, 88, 126)']],
    'histfunc': 'avg',
    'histnorm': 'percent',
    'hovertemplate': 'Sim Period=%{x}<br>Annualised Return=%{y}<br>count=%{z}<extra></extra>',
    'name': '',
    'nbinsx': 209,
    'showscale': True,
    'x': array([ 52,  53,  54, ..., 258, 259, 260], dtype=int64),
    'xaxis': 'x',
    'xbingroup': 'x',
    'y': array([0.13911654, 0.14298002, 0.14947634, ..., 0.07539636, 0.07368169,
                0.07359861]),
    'yaxis': 'y',
    'ybingroup': 'y'
})

而且似乎也没有一个选择

fig.update_layout(zaxis=())

谢谢你的帮助

编辑: screen shot of data frame


Tags: returnfigupdatergbsimperiodavgpercent