vestland
vestland

Reputation: 61234

Plotly: How to inspect and make changes to a plotly figure?

Related questions have already been asked and before e.g.

But the answers to these questions have been limited by the fact that not all parameters have been available through Python, meaning that the real answers were buried somewhere in JavaScript. But for newer versions of plotly, how can you inspect and edit a plotly figure? How could you, for example, find out what the background color of a figure is? And then change it? From the second link above you can see that fig.show and print(fig) will reveal some details about the figure structure. But certainly not all of it. The code snippet below will produce the following plot:

Plot:

enter image description here

Code:

import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()

Running fig.show will now partly reveal the structure of the figure in the form of a dict:

<bound method BaseFigure.show of Figure({
    'data': [{'hovertemplate': 'year=%{x}<br>lifeExp=%{y}<extra></extra>',
              'legendgroup': '',
              'line': {'color': '#636efa', 'dash': 'solid'},
              'mode': 'lines',
              'name': '',
              'orientation': 'v',
              'showlegend': False,
              'type': 'scatter',
              'x': array([1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007],
                         dtype=int64),
              'xaxis': 'x',
              'y': array([68.75 , 69.96 , 71.3  , 72.13 , 72.88 , 74.21 , 75.76 , 76.86 , 77.95 ,
                          78.61 , 79.77 , 80.653]),
              'yaxis': 'y'}],
    'layout': {'legend': {'tracegroupgap': 0},
               'template': '...',
               'title': {'text': 'Life expectancy in Canada'},
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'year'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'lifeExp'}}}
})

But as you can see for yourself, there are a lot of details missing. So how can you peform a more complete figure introspection?

Upvotes: 2

Views: 1109

Answers (1)

vestland
vestland

Reputation: 61234

As of version 4.10, the plotly developers have introduced the awesome fig.full_figure_for_development() function which they talk about here. There you'll see that:

fig.full_figure_for_development() function will return a new go.Figure object, prepopulated with the same values you provided, as well as all the default values computed by Plotly.js, to allow you to learn more about what attributes control every detail of your figure and how you can customize them. This function is named “for development” because it’s not necessary to use it to produce figures, but it can be really handy to explore figures while you’re figuring out how to build them.

So building on the example in the question, the following snippet will produce an output of about 180 lines containing, among a plethora of other details, this part about the figure layout:

'margin': {'autoexpand': True, 'b': 80, 'l': 80, 'pad': 0, 'r': 80, 't': 100},
'modebar': {'activecolor': 'rgba(68, 68, 68, 0.7)',
           'bgcolor': 'rgba(255, 255, 255, 0.5)',
           'color': 'rgba(68, 68, 68, 0.3)',
           'orientation': 'h'},
'newshape': {'drawdirection': 'diagonal',
            'fillcolor': 'rgba(0,0,0,0)',
            'fillrule': 'evenodd',
            'layer': 'above',
            'line': {'color': '#444', 'dash': 'solid', 'width': 4},
            'opacity': 1},
'paper_bgcolor': 'white',
'plot_bgcolor': '#E5ECF6',
'separators': '.,',
'showlegend': False,
'spikedistance': 20,

And there you can also see the background color of the plot as 'plot_bgcolor': '#E5ECF6'. And you probably know that you can set the background color using to for example 'grey' using fig.update_layout(plot_bgcolor='grey'). But now you know how to get it as well:

# In:
fig.layout.plot_bgcolor

# Out:
'#E5ECF6'

And in knowing how to do this, you know how to get and set almost any attribute of a plotly figure. And it doesn't matter if you've built the figure using plotly.graph_objects or plotly.express

Upvotes: 3

Related Questions