How to obtain generated x-axis and y-axis range in plotly plot?
Asked Answered
B

4

26

I have a very simple bubble chart, see below. the only thing i need is to be able to get the range (or the min and max) or the x and y axis generated.

trace = go.Scatter(
    x=df_test['total_points_mean'],
    y=df_test['total_points_std'],
    mode='markers',
    text=df_test['play_maker'],
    marker=dict(size=df_test['week_nunique'],
                color = df_test['week_nunique'],
                showscale=True)
)

layout = go.Layout(title='Scatter Plot')
fig = go.Figure(data=[trace],layout=layout)

From the resulting plot, the min and max of the x axis seems to be around ~10 and ~29, but i need a way to generate the exact value of the axis range.

enter image description here

Is there a way to access the generated axes range?

Booboo answered 26/5, 2020 at 20:19 Comment(0)
B
34

Getting the axis range from a plotly plot is not possible within the python implementation. You can only retrieve the range if you specify the axis range in your layout (but then you really don't need it).

So if you try print(fig.layout.xaxis.range) you will get None.

If you need the limits then you'll need to make your own and apply it to the layout:

  1. get the min and max of the x-values: xmin, xmax
  2. pad these values with some factor: xlim = [xmin*.95, xmax*1.05]
  3. update the layout: fig.update_layout(xaxis=dict(range=[xlim[0],xlim[1]]))

Now if you try print(fig.layout.xaxis.range) you'll get the axis range.

This was bugging me a great deal so I had to dig deeper, credit goes to @Emmanuelle on the plotly forums for confirming this reality.

UPDATE 20210129: Plotly has added .full_figure_for_development() method.

The .full_figure_for_development() method provides Python-level access to the default values computed by Plotly.js. This method requires the Kaleido package, which is easy to install and also used for static image export.

So now you can:

full_fig = fig.full_figure_for_development()
print(full_fig.layout.xaxis.range)
Brandon answered 27/5, 2020 at 11:32 Comment(3)
Seems odd that such a simple feature is not allowed, but in any case, thank you!Booboo
looks like full_figure_for_development() fails for FigureWidget with AttributeError: 'FigureWidget' object has no attribute 'get'Bearden
Thanks. How to label each y axis in subplots. like in row=2, primary and secondary y axis? ThanksCauvery
L
5

If you have no access to the input data, but to the trace:

x_min = min(trace.x)
x_max = max(trace.x)

If you have no access to the trace but to the figure handle the following should work (I assume this is the usual case):

x_mins = []
x_maxs = []
for trace_data in fig.data:
    x_mins.append(min(trace_data.x))
    x_maxs.append(max(trace_data.x))
x_min = min(x_mins)
x_max = max(x_maxs)

I assume fig.layout.xaxis.range is None if the axis range is automatic.

Lollapalooza answered 24/9, 2020 at 12:53 Comment(2)
The answer of the user that needed the kaleido package didn't work for me, instead your second method did! In the case you only have one trace and just need all the values of the x axis is even simpler, with fig.data[0]['x'] you we'll get all of them as a listSequence
Commenting to confirm that this solution worked for me too, but only for the y-axis using fig.data[0]['y'].max() When I tried to get x-axis values using fig.data[0]['x'].min() and fig.data[0]['x'].max() it gave me incorrect values.Topsyturvy
P
0

There is a way to access the X axis on your fig or even after zooming

You'll have to output your fig to html and there will be a class called xtick enter image description here

Those data-unformatted under xtick represents the x-axes of your fig

You can use beautiful soup or search to scale through the html to get that

Pensive answered 6/1, 2021 at 7:16 Comment(0)
E
-1

I had a similar case with the y-axis. I went to the plot data, checked each plotted series and retrieved it from there.

plot_min = fig.data[0].y.min()
plot_max = fig.data[0].y.max()

for plot_series in fig.data:
    plot_series_min = plot_series.y.min()
    plot_series_max = plot_series.y.max()

    if plot_series_min < plot_min:
        plot_min = plot_series_min

    if plot_series_max > plot_max:
        plot_max = plot_series_max

Zooming won't update these values tho.

Esp answered 14/1, 2022 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.