繁体   English   中英

在 Plotly 中隐藏图例形式的条形图标签

[英]Hide the legend form barchart label in Plotly

我处理简单的问题,在标签中添加 Y 值的颜色映射后,添加了对我来说没用的新值。 有什么办法可以删除这个吗?

        fig1=px.bar(europe_df1, x="Month End Date", y="Monthly Return",
        color=europe_df1["Monthly Return"] > 0,color_discrete_map= 
        {True: "green", False: "red"},)
        fig1.update_layout({
        'plot_bgcolor':' rgba(0, 0, 0, 0)',
        'paper_bgcolor': 'rgba(0, 0, 0, 0)',
        'font_color':'white',
        'showlegend':False,
        })

我想删除“颜色”标签,只保留 X 和 Y 值。

可能还有另一种最好的解决方案,但我们可以检索图形数据并对其进行更新以排除不必要的字符串。 由于没有提供数据,我已经修改了官方参考中的示例。

import plotly.express as px

data_canada = px.data.gapminder().query("country == 'Canada'")

fig = px.bar(data_canada,
             x='year',
             y='pop',
             color=data_canada['pop'] > 20_000_000,
             color_discrete_map={True: "green", False: "red"},
            )
fig.update_layout({
        'plot_bgcolor':' rgba(0, 0, 0, 0)',
        'paper_bgcolor': 'rgba(0, 0, 0, 0)',
        'font_color':'white',
        'showlegend':False,
        })

hovertext = fig.data[0]['hovertemplate']
htext = hovertext.split('<br>',1)
#print(htext)
fig.data[0]['hovertemplate'] = htext[1]
fig.data[1]['hovertemplate'] = htext[1]
fig.show()

在此处输入图像描述

如果没有进行其他设置

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM