deeenes
deeenes

Reputation: 4586

Plotly python: force axis limits (range)

Documentation of plotly says, with parameter range we can set the limits of the axis, for example range = [0, 10] sets the axis minimum to 0 and maximum to 10. According to docs, it can be used under figure, layout, scene or xaxis. Indeed Scatter and Layout accepts xaxis and yaxis arguments, which are either dicts or plotly.graph_objs.XAxis objects, and there it is possible to supply a range value. However, with trying all the variations I can imagine, it apparently fails to set the limits. In addition, the first subplot appears to be below the others. See the minimum working example below, which can be run in any Jupyter notebook.

Bonus question: why fill argument at Scatter fails to set the fill color of the dots?

import plotly
import plotly.tools
import plotly.graph_objs
plotly.offline.init_notebook_mode()

data = {
    'A': {
        'x': [1.0, 2.0, 6.0, 8.0],
        'y': [34.0, 36.0, 38.0, 40.0],
        's': [0.00416, 0.01125, 0.0038, 0.002]
    },
    'B': {
        'x': [1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 3.0, 2.0, 2.0, 3.0,
            4.0, 2.0, 4.0, 5.0, 6.0, 7.0, 7.0],
        'y': [30.0, 32.0, 32.0, 33.0, 34.0, 34.0, 34.0, 35.0,
            36.0, 36.0, 36.0, 38.0, 38.0, 38.0, 38.0, 38.0, 40.0],
        's': [0.029999999999999999, 0.19625000000000001, 0.070833333333333331,
            0.0079166666666666673, 0.23749999999999999, 0.37708333333333333,
            0.028333333333333332, 0.018749999999999999, 0.51875000000000004,
            0.066666666666666666, 0.02375, 0.0066666666666666671,
            0.012083333333333333, 0.01125, 0.016666666666666666,
            0.0058333333333333336, 0.0275]
    },
    'C': {
        'x': [1.0, 2.0, 1.0, 2.0, 2.0],
        'y': [32.0, 32.0, 34.0, 34.0, 36.0],
        's': [0.029208333333333333, 0.0050000000000000001, 0.03820833333333333,
            0.022833333333333334, 0.029083333333333333],
    },
    'D': {
        'x': [0.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 3.0, 1.0, 1.0,
            2.0, 3.0, 4.0, 5.0, 6.0, 3.0, 4.0, 5.0, 6.0, 7.0, 6.0, 7.0],
        'y': [30.0, 30.0, 31.0, 31.0, 32.0, 32.0, 33.0, 33.0, 34.0, 34.0, 34.0,
            35.0, 36.0, 36.0, 36.0, 36.0, 36.0, 36.0, 38.0, 38.0, 38.0, 38.0,
            38.0, 40.0, 40.0],
        's': [0.0087500000000000008, 0.050000000000000003, 0.008750000000000000,
            0.013333333333333334, 0.60875000000000001, 0.16666666666666666,
            0.04583333333333333, 0.00070833333333333338, 0.73320833333333335,
            0.54541666666666666, 0.040833333333333333, 0.02, 0.0700000000000,
            0.73124999999999996, 0.1125, 0.066666666666666666, 0.02083333332,
            0.0083333333333333332, 0.027916666666666666, 0.0212500000000000,
            0.070833333333333331, 0.11666666666666667, 0.040833333333333333,
            0.059999999999999998, 0.1125]
    }
}

xlim = [0, 9]
ylim = [13, 60]

traces = []

for name in sorted(data.keys()):
    sub = data[name]

    traces.append(plotly.graph_objs.Scatter(x = sub['x'], y = sub['y'],
                                        mode = 'markers',
                                        marker = dict(
                                            size = sub['s'],
                                            sizemode = 'area',
                                            sizeref = 0.0001),
                                        name = name,
                                        fill = '#333333',
                                        showlegend = False,
                                        xaxis = dict(range = xlim),
                                        yaxis = dict(range = ylim))
                                    )

fig = plotly.tools.make_subplots(rows=2,
                                cols=2,
                                subplot_titles=sorted(data.keys())
                            )

for i, trace in enumerate(traces):
    fig.append_trace(trace, row = i // 2 + 1, col = (i % 2) + 1)

fig['layout'].update(height = 1000, width = 600, title = main_title,
                    xaxis = dict(range = xlim), yaxis = dict(range = ylim))

plotly.offline.iplot(fig, show_link = False)

Result looks like this:

failed to set axes limits

Upvotes: 7

Views: 22899

Answers (1)

saaj
saaj

Reputation: 25283

It looks like it's working as expected in plotly=5.1.0.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=2, subplot_titles=sorted(data.keys()))
for i, name in enumerate(sorted(data.keys())):
    sub = data[name]
    fig.add_trace(
        go.Scatter(
            x = sub['x'], 
            y = sub['y'],
            mode = 'markers',
            marker = dict(
                size = sub['s'],
                sizemode = 'area',
                sizeref = 0.0001,
            ),
            name = name,
            showlegend = False,
        ),
        row = i // 2 + 1,
        col = (i % 2) + 1,        
    )
fig.update_xaxes(range=[0, 9])
fig.update_yaxes(range=[13, 60])  
fig

scatter grid

Upvotes: 9

Related Questions