Building a task on a log scale in matplotlib in python

I am trying to plot the following numbers on a logarithmic scale as a scatter plot in matplotlib. Both values ​​on the x and y axes have very different scales, and one of the variables has a huge dynamic range (approximately from 0 to 12 million approximately), and the other between 0 and 2. I think this can be good for building both log scale.

I tried the following: for a subset of the values ​​of 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-axis is displayed on a log scale, but the points are not displayed - only two points appear. Any idea how to fix this? Also, how can I make this scale scale on the square axis, so that the correlation between the two variables can be interpreted from the scatter plot?

thank.

+3
source share
2 answers

I don’t know why you only get these two points. In this case, you can manually adjust the limits to make sure all of 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 what “Also, how can I make this scale scale on the square axis, 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?

+3
source

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 gives the graph that you want with correctly scaled axes, although it does not have all the flexibility of a true scatter plot.

+2

Source: https://habr.com/ru/post/1742374/


All Articles