Skip to content Skip to sidebar Skip to footer

Matplotlib Scatterplot Axis Autoscale Fails For Small Data Values

When using Matplotlib's scatterplot, sometimes autoscaling works, sometimes not. How do I fix it? As in the example provided in the bug report, this code works: plt.figure() x = np

Solution 1:

In at least Matplotlib 1.5.1, there is a bug where autoscale fails for small data values, as reported here.

The workaround is to use .set_ylim(bottom,top) (documentation) to manually set the data limits (in this example for the y axis, to set the x axis, use .set_xlim(left,right).

To automatically find the data limits that are pleasing to the eyes, the following pseudocode can be used:

defset_axlims(series, marginfactor):
    """
    Fix for a scaling issue with matplotlibs scatterplot and small values.
    Takes in a pandas series, and a marginfactor (float).
    A marginfactor of 0.2 would for example set a 20% border distance on both sides.
    Output:[bottom,top]
    To be used with .set_ylim(bottom,top)
    """
    minv = series.min()
    maxv = series.max()
    datarange = maxv-minv
    border = abs(datarange*marginfactor)
    maxlim = maxv+border
    minlim = minv-border

    return minlim,maxlim

Post a Comment for "Matplotlib Scatterplot Axis Autoscale Fails For Small Data Values"