Dexter
Dexter

Reputation: 11831

Plot issue while changing axes scales on matplotlib scatter plot

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

Upvotes: 0

Views: 1530

Answers (2)

tacaswell
tacaswell

Reputation: 87356

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.

Upvotes: 0

user707650
user707650

Reputation:

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.

Upvotes: 1

Related Questions