1

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.array([0,1,2,3])
x = np.array([2,4,5,9])
plt.scatter(x,y)

But when using smaller values, the scaling fails to work:

plt.figure()
x = np.array([0,1,2,3])
x = np.array([2,4,5,9])
plt.scatter(x/10000,y/10000)

Edit: An example can be found here. I have not specified the specific cause in the question, because when encountering the error it is not obvious what causes it. Also, I have specified the solution and cause in my own answer.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
harbun
  • 515
  • 6
  • 23

1 Answers1

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:

def set_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
harbun
  • 515
  • 6
  • 23