legends 的 plotly 文档提供有关隐藏图例条目的信息:
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
生成我的图。所以,我已经有了一个 plotly 图,因此没有为每条轨迹设置 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))
我想隐藏列列表。
columns_to_hide = ['A', 'C']
我该怎么做?
最佳答案
您可以在绘图前设置图形的任何参数,如下所示:
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)
关于python - 隐藏图例中的图例条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526734/