0

I am drawing a scatter plot using matplotlib. This is a somewhat peculiar issue. Here's the plot of the data when there is no scale of the axes

plt.scatter(x, y , marker ='x')
#plt.xscale('log')
#plt.yscale('log')
plt.show()

enter image description here

The plot of the data with axes scaled to `logarithm' scale.

enter image description here

Why is this happening? The same happens even when the base is changed to 2 or e

2
  • While this probably shouldn't happen indeed, it appears there are values of 0 in your data. Could you first try and remove those, then see if the problem still occurs? If not 0 or lower, I assume they are very small, but not below the floating point precision? Commented Dec 2, 2013 at 14:00
  • @Evert - I tried removing the 0's from the data and drawing the plots but the issue persists. Commented Dec 2, 2013 at 14:11

2 Answers 2

1

It appears that in this particular case, you can't scale the axes after the plot. You can scale them beforehand, so:

plt.xscale('log')
plt.yscale('log')
plt.scatter(x, y , marker ='x')
plt.show()

In this particular case (identical markers), you could also use plt.plot instead of plt.scatter, and then scaling post-plot will work:

plt.plot(x, y, 'x')
plt.xscale('log')
plt.yscale('log')
plt.show()

My guess as to why you can't scale post scatter plot: a scatter plot returns a PathCollection, which probably results in the scaling function looking only at the last entry of that collection, that is, the last point. That would of course scale only in the range 1e5 - 1e6. plot() returns a Line2D, which takes the complete plot into account.

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

3 Comments

Woaah! Thanks - this worked. I am not sure if this should be a bug though.
Yup, could be seen as a bug (or at least as a convenience to users if this works both ways). Perhaps ask on the mpl mailing list?
@Evert If it were the case the the limit scaling only looked at the last point of the PathCollection, then no auto-scaling on scatter plots would work.
0

The log scaling should clip values sensibly so 0 in the data are not the problem.

I suspect the issue is that your limits have negative values so naively taking the log of the limits and using those causes issues.

You can also explicitly set the limit

ax = plt.gca()
ax.set_xlim([1e-5, 1e6])
ax.set_ylim([1e-5, 1e6])

and not rely on auto-scaling.

4 Comments

Not really, check the other answer.
@Dexter I don't understand your comment.
I didn't check your comment to Evert May be, I am wrong. I will soon write to the MPL mailing list and find out the reason.
@tcaswell You're saying that mpl auto-scales the linear data so that the axes have negative limits, and then attempts to take the log of those limits? While logical, I'd still file the behaviour as a bug. You're right that the PathCollection has nothing to do with it then.

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.