How to hide legend with Plotly Express and Plotly

时间秒杀一切 提交于 2019-12-12 11:19:25

问题


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.

And my code

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


回答1:


try this:

    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)
  • 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

Hope it works for you.




回答2:


The issue with your original code is that fig.update() doesn't take fig as an argument. That line could just be fig.update(layout_showlegend=False)



来源:https://stackoverflow.com/questions/56712486/how-to-hide-legend-with-plotly-express-and-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!