我正在尝试学习Plotly,首先在Plotly Express中创建一个简单的条形图,然后用Plotly更新它以完善它。我想隐藏这个传说。
我试图通过隐藏图例来更新原始图形,但我无法使其工作。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。基本上,我希望右边那个可怕的传奇消失
发布于 2019-06-22 12:44:05
试试这个:
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
是layout对象的一部分,你没有在你的代码中指定my_layout
包装在go.Layout()
中,代码也可以工作。它可以通过简单的保持一个字典my_layout
plotly.express
,试试fig.update_layout(showlegend=False)
。所有的论点都是一样的。访问它们的方式略有不同。希望它能为你工作。
发布于 2020-06-30 08:38:29
在plotly中创建图形后,要禁用图例,可以使用以下命令:
fig.update_layout(showlegend=False)
对于高级用户:您还可以通过设置每个跟踪的showlegend属性来启用/禁用地物中单个跟踪的图例。例如:
fig.add_trace(go.Scatter(
x=[1, 2],
y=[1, 2],
showlegend=False))
您可以在此处查看示例:https://plotly.com/python/legend/
发布于 2019-06-23 08:58:43
原始代码的问题是fig.update()
没有将fig
作为参数。这行代码可能就是fig.update(layout_showlegend=False)
https://stackoverflow.com/questions/56712486
复制相似问题