Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to inspect the basic figure structure (version 4)

Tags:

python

plotly

For older versions of plotly, for example in Jupyterlab, you could simply run figure to inspect the basics of your figure like this:

Ouput:

{'data': [{'marker': {'color': 'red', 'size': '10', 'symbol': 104},
   'mode': 'markers+lines',
   'name': '1st Trace',
   'text': ['one', 'two', 'three'],
   'type': 'scatter',
   'x': [1, 2, 3],
   'y': [4, 5, 6]}],
 'layout': {'title': 'First Plot',
  'xaxis': {'title': 'x1'},
  'yaxis': {'title': 'x2'}}}

Code for versions prior to V4:

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104, 'size': "10"},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')

data=go.Data([trace1])
layout=go.Layout(title="First Plot", xaxis={'title':'x1'}, yaxis={'title':'x2'})
figure=go.Figure(data=data,layout=layout)
#py.iplot(figure, filename='pyguide_1')
figure

If you do the same thing now with a similar setup, the same approach will not produce the figure basics, but rather plot the figure itself:

Code:

import pandas as pd
import plotly.graph_objects as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')  

figure = go.Figure(data=trace1)  

figure

Output:

enter image description here

In many ways this is similar to how you for example would build and plot a figure with ggplot in R. And since plotly is available for both R and Python I thinks this makes sense after all. But I'd really like to know how to access that basic setup.

What I've tried:

I think this change is due to the fact that figure is now a plotly.graph_objs._figure.Figure and used to be a dictionary(?). So figure['data'] and figure['layout'] are still dicts with necessary and interesting content:

Output from figure['data']

(Scatter({
     'marker': {'color': 'red', 'symbol': 104},
     'mode': 'markers+lines',
     'name': '1st Trace',
     'text': [one, two, three],
     'x': [1, 2, 3],
     'y': [4, 5, 6]
 }),)

Output from figure['layout']

Layout({
    'template': '...'
})

And of course options such as help(figure) and dir(figure) are helpful, but produces a very different output.

like image 805
vestland Avatar asked Aug 05 '26 11:08

vestland


1 Answers

I just found out that 'forgetting' the brackets for figure.show() will give me exactly what I'm looking for. So with a setup similar to the code in the question and with plotly V4, simply running figure.show will give you this:

Output:

<bound method BaseFigure.show of Figure({
    'data': [{'marker': {'color': 'red', 'symbol': 104},
              'mode': 'markers+lines',
              'name': '1st Trace',
              'text': [one, two, three],
              'type': 'scatter',
              'x': [1, 2, 3],
              'y': [4, 5, 6]}],
    'layout': {'template': '...'}
})>

Code:

import pandas as pd
import plotly.graph_objects as go

trace1 = go.Scatter(x=[1,2,3], y=[4,5,6], marker={'color': 'red', 'symbol': 104},
                    mode="markers+lines",  text=["one","two","three"], name='1st Trace')  

figure = go.Figure(data=trace1)  

figure.show
like image 54
vestland Avatar answered Aug 08 '26 02:08

vestland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!