0

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

OK that's a fix, but is that the way it's supposed to work? It only affects the y scale, the x scale does not need this.
I guess I must have always been doing it in the 'right' order without realizing it. Thanks!
1

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.

4 Comments

y, but not x? I use log constantly, sometimes x, or y, or both axes. I've never seen this happen before.
hmm... like I said in the other answer, possibly I've always been doing it in that order without actually realizing that it was the 'right' order.
Thanks! I looked back at some examples of my plots and I found that 90% of the time I use plt.plot() where the 'wrong' order works just fine. It seems it's 'wrong' for plt.scatter(). A lot of my scatter plots that use log scales seemed to have worked 'by accident' due to limited range - like this one just for a random example. And possibly this has happened occasionally before and I just fiddled with it, got it working, and moved on.
I've linked your helpful answer in this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.