plotly中trace标签去除

第一次使用plotly绘制图像,效果很惊艳,但是也有很多问题,比如标签时怎么生成的,分类是根据什么来的,看了一天的文档看得一脸懵逼…同时遇到了add_trace后出现trace标签的问题,给出原来的代码和改进后的代码,以供参考。

fig = make_subplots(rows=1, cols=2, specs=[[{"type": "pie"}, {"type": "bar"}]])
colors = ['gold', 'mediumturquoise', 'lightgreen'] 
# 绘制饼状图
fig.add_trace(go.Pie(labels=df.label_name.value_counts().index,
                             values=df.label.value_counts().values,name="pie"), row=1, col=1)

fig.update_traces(hoverinfo='label+percent', textfont_size=20,
                  marker=dict(colors=colors, line=dict(color='#000000', width=2)))
# 绘制柱状图
fig.add_trace(go.Bar(x=df.label_name.value_counts().index, y=df.label.value_counts().values, 
                     marker_color=colors),row=1,col=2)
fig.show()

注意看右侧legend,会多出一个trace1,这个trace1是add_trace(go.Bar())带来的。
在这里插入图片描述

改后的代码:

fig = make_subplots(rows=1, cols=2, specs=[[{"type": "pie"}, {"type": "bar"}]])
colors = ['gold', 'mediumturquoise', 'lightgreen'] 
# 绘制饼状图
fig.add_trace(go.Pie(labels=df.label_name.value_counts().index,
                             values=df.label.value_counts().values,name="pie"), row=1, col=1)

fig.update_traces(hoverinfo='label+percent', textfont_size=20,
                  marker=dict(colors=colors, line=dict(color='#000000', width=2)))
# 绘制柱状图
fig.add_trace(go.Bar(x=df.label_name.value_counts().index, y=df.label.value_counts().values, 
                     marker_color=colors),row=1,col=2)
# 选择第二个trace,并将其设置为不显示(官方文档看得脑壳疼啊)
fig.update_traces(selector=dict(type='bar'),showlegend=False)
fig.show()

在这里插入图片描述
这是官网给出的方法,可能是我没有看懂,麻烦看懂的也给解释一下:

fig.update_traces(hovertemplate=<VALUE>, selector=dict(type='bar'))
Anything contained in tag `<extra>` is displayed in the secondary box,
for example "<extra>{fullData.name}</extra>". To hide the secondary box 
completely, use an empty tag `<extra></extra>`.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值