隐藏图形图中的图例条目

时间:2017-08-05 22:00:16

标签: python pandas legend plotly

legends的情节文档提供了隐藏图例条目的信息:

//create view then add a tag to it. The tag references the view
var myNewView = UIView()
myNewView.tag = 100

//add the  view you just created to the window
window.addSubview(myNewView)

//remove the view you just created from the window. Use the same tag reference
window.viewWithTag(100)?.removeFromSuperview

然而,我正在从import plotly.plotly as py import plotly.graph_objs as go trace0 = go.Scatter( x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5], showlegend=False ) trace1 = go.Scatter( x=[1, 2, 3, 4, 5], y=[5, 4, 3, 2, 1], ) data = [trace0, trace1] fig = go.Figure(data=data) py.iplot(fig, filename='hide-legend-entry') 生成我的情节。所以,我已经有了一个阴谋图,因此没有为每条迹线设置DataFrame的自由。

showlegend=False

我想要隐藏列列表。

import pandas as pd
import plotly.offline as py
import cufflinks as cf
cf.go_offline()

df = pd.DataFrame(data=[[0, 1, 2], [3, 4, 5]], columns=['A', 'B', 'C'])
py.plot(df.iplot(kind='scatter', asFigure=True))

我该怎么做?

2 个答案:

答案 0 :(得分:6)

您可以在绘制之前设置图形的任何参数,如下所示:

import pandas as pd
import plotly.offline as py
import plotly.graph_objs as go
import cufflinks as cf
cf.go_offline()

df = pd.DataFrame(data=[[0, 1, 2], [3, 4, 5]], columns=['A', 'B', 'C'])

# get figure property
fig = df.iplot(kind='scatter', asFigure=True)

# set showlegend property by name of trace
for trace in fig['data']: 
    if(trace['name'] != 'B'): trace['showlegend'] = False

# generate webpage
py.plot(fig)

答案 1 :(得分:2)

不确定这是否是最近添加的,但是在当前版本的plotly(4.0及更高版本)中,您可以执行fig.update(layout_showlegend=False)