user248237
user248237

Reputation:

problem plotting on logscale in matplotlib in python

I am trying to plot the following numbers on a log scale as a scatter plot in matplotlib. Both the quantities on the x and y axes have very different scales, and one of the variables has a huge dynamic range (nearly 0 to 12 million roughly) while the other is between nearly 0 and 2. I think it might be good to plot both on a log scale.

I tried the following, for a subset of the values of the two variables:

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_yscale('log')
ax.set_xscale('log')
plt.scatter([1.341, 0.1034, 0.6076, 1.4278, 0.0374],
        [0.37, 0.12, 0.22, 0.4, 0.08])

The x-axes appear log scaled but the points do not appear -- only two points appear. Any idea how to fix this? Also, how can I make this log scale appear on a square axes, so that the correlation between the two variables can be interpreted from the scatter plot?

thanks.

Upvotes: 3

Views: 11690

Answers (2)

tom10
tom10

Reputation: 69172

You can also just do,

plt.loglog([1.341, 0.1034, 0.6076, 1.4278, 0.0374], 
                     [0.37, 0.12, 0.22, 0.4, 0.08], 'o')

This produces the plot you want with properly scaled axes, though it doesn't have all the flexibility of a true scatter plot.

Upvotes: 2

Mike Graham
Mike Graham

Reputation: 76663

I don't know why you only get those two points. For this case, you can manually adjust the limits to make sure all your points fit. I ran:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 8)) # You were missing the =
ax = fig.add_subplot(1, 1, 1)
ax.set_yscale('log')
ax.set_xscale('log')
plt.scatter([1.341, 0.1034, 0.6076, 1.4278, 0.0374],
        [0.37, 0.12, 0.22, 0.4, 0.08])
plt.xlim(0.01, 10) # Fix the x limits to fit all the points
plt.show()

I'm not sure I understand understand what "Also, how can I make this log scale appear on a square axes, so that the correlation between the two variables can be interpreted from the scatter plot?" means. Perhaps someone else will understand, or maybe you can clarify?

Upvotes: 3

Related Questions