如何使用Plotly Express和Plotly隐藏图例 [英] How to hide legend with Plotly Express and Plotly

查看:945
本文介绍了如何使用Plotly Express和Plotly隐藏图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过首先在Plotly Express中创建一个简单的条形图,然后使用Plotly对其进行更新以精巧地学习Plotly.我想隐藏传说.

I am trying to learn Plotly by firstly creating a simple bar chart in Plotly Express and then updating it with Plotly to finesse it. I would like to hide the legend.

我正试图通过隐藏图例来更新原始图形,但无法使其正常工作. 这是我的回溯错误.

I am trying to update the original figure by hiding the legend, and I can't get it to work. This is my traceback error.

还有我的代码

import plotly.plotly as py
import plotly.graph_objs as go
import plotly_express as px
import pandas as pd


df = pd.read_csv('C:/Users/Documents/Python/CKANMay.csv')

fig = px.bar(df, x="Publisher", y="Views", color="Publisher", barmode="overlay")

fig.update(fig, showlegend="false")

这是带有图例的图表现在的样子.基本上,我希望右边的那个可怕的传说消失

This is what the chart looks like now with the legend. Basically, I want that awful legend on the right to go away

推荐答案

尝试一下:

    my_data = [go.Bar( x = df.Publisher, y = df.Views)]
    my_layout = go.Layout({"title": "Views by publisher",
                           "yaxis": {"title":"Views"},
                           "xaxis": {"title":"Publisher"},
                           "showlegend": False})

    fig = go.Figure(data = my_data, layout = my_layout)

    py.iplot(fig)

  • 参数showlegend是您未在代码中指定的布局对象的一部分
  • 如果未将布局对象my_layout包装在go.Layout()中,则该代码也可能起作用.只需保留my_layout一本字典
    • the argument showlegend is part of layout object which you did not specify in your code
    • The code may also work if you do not wrap the layout object my_layout inside a go.Layout(). It could work by simply keeping my_layout a dictionary
    • 希望它对您有用.

      这篇关于如何使用Plotly Express和Plotly隐藏图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆