简体   繁体   中英

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?

在此处输入图片说明

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()

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.)

It seems like matplotlib is creating the y-axis ticks before converting to a log scale, and then not recreating the ticks based on the change. The y-axis on your first subplot starts at 10e1, not 10e-3. So change the scales before you plot.

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

I think if you plot the original scale beside the log scale, you might be able to figure out the answer to the partial treatment of the axes by matplotlib . In a log scale, there is no true 0 -- because log(0) is undefined. So the coordinate has to start somewhere above 0, and that causes the problems. Your x axis ranges from 0 to 3, but y from 0 to 16. When converted to log, matplotlib correctly scales x axis, but since y has a factor of 10, it misses the scaling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM