uhoh uhoh - 3 months ago 17
Python Question

Why does matplotlib choose the wrong range in y using log scale?

Using matplotlib version 1.5.1 and python 2.7.11 I noticed that I need to specify the limits in y manually or else only the largest y-value point is plotted. Arrays behave the same way.

If I remove the first point, I get a few more points, but not all of them.

I don't recall ever having to manually set limits like this before - why here?

enter image description here

import matplotlib.pyplot as plt

X = [0.997, 2.643, 0.354, 0.075, 1.0, 0.03, 2.39, 0.364, 0.221, 0.437]
Y = [15.487507, 2.320735, 0.085742, 0.303032, 1.0, 0.025435, 4.436435,
0.025435, 0.000503, 2.320735]

plt.figure()

plt.subplot(1,2,1)
plt.scatter(X, Y)
plt.xscale('log')
plt.yscale('log')


plt.subplot(1,2,2)
plt.scatter(X, Y)
plt.xscale('log')
plt.yscale('log')
plt.ylim(0.5*min(Y), 2.0*max(Y)) # why is this line necessary?
plt.title('added plt.ylim()')

plt.show()

Answer

The problem arises because you have first drawn the scatter plot and then set the scales as logarithmic which results in a zooming in effect. This removes the problem:

plt.xscale('log')
plt.yscale('log')
plt.scatter(X, Y)

This produces the intended result. (2nd subplot in your question.)

Comments