Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide legend entries in a plotly figure

The plotly docs for legends provides info on hiding legend entries:

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

However, I'm generating my plot from a DataFrame. So, I already have a plotly figure, and therefore don't have the liberty of setting showlegend=False for each trace.

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

I want to hide a list of columns.

columns_to_hide = ['A', 'C']

How can I do this?

like image 800
bluprince13 Avatar asked Aug 05 '17 22:08

bluprince13


People also ask

How do you hide legends in plotly?

In this example, we are hiding legend in Plotly with the help of method fig. update(layout_showlegend=False), by passing the showlegend parameter as False.

How do you delete a trace in plotly?

Option 1 (tell plotly to delete the trace(s) in question): Plotly. deleteTraces(graphDiv, 0); where the second argument is the trace index of the trace to delete.

How do you remove gridlines in plotly?

Axis grid lines can be disabled by setting the showgrid property to False for the x and/or y axis.

What is Add_trace in plotly?

Adding Traces To Subplots If a figure was created using plotly. subplots. make_subplots() , then supplying the row and col arguments to add_trace() can be used to add a trace to a particular subplot.


1 Answers

You can set any parameters of figure before plotting like here:

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)
like image 160
Serenity Avatar answered Sep 22 '22 05:09

Serenity