I have a decreasing function $y\colon [1,3600] \to \mathbb{R}^+$.
I try to set the logarithmic scale for the y-axis, but if the range of the function is small (i.e., the initial and final value are closeby), then the logarithmic scale does not work and it only prints the linear scale.
Code for reproduction
importrandomimportmatplotlib.pyplotaspltT=3600n=1000y_start=10**4y_end=0.9*y_startrandom.seed(42)
# Generate n increasing random values between 1 and Tx= [random.randrange(1, T) for_inrange(n)]
x.sort()
# Generate n decreasing random values between y_start and y_endy= [random.uniform(y_end, y_start) for_inx]
y.sort(reverse=True)
fig, ax=plt.subplots()
ax.set_yscale('log')
ax.plot(x, y)
plt.savefig('step_function.pdf')
When the deviation is sufficiently small, a logarithmic scale does look linear, that just is how the scale works. If you do not have data that scales multiple orders of magnitude, then a log scale is not likely what you want.
As far as I can tell, this is working as expected.
When you are selecting values between 0.9 and 1.0 times your power of 10, as in this example, you are using only ~5% of one decade.
When you change that to 0.2 to 1.0 times your power of 10, that increases to ~70%, which is enough to see the deviations. (Though still not actually spanning multiple orders of magnitude, where logarithmic really shines).
Reacted by Tommaso Mannelli Mazzoli and Jody Klymak
Activity
ksunden commented on Jan 30, 2025
When the deviation is sufficiently small, a logarithmic scale does look linear, that just is how the scale works. If you do not have data that scales multiple orders of magnitude, then a log scale is not likely what you want.
As far as I can tell, this is working as expected.
When you are selecting values between 0.9 and 1.0 times your power of 10, as in this example, you are using only ~5% of one decade.
When you change that to 0.2 to 1.0 times your power of 10, that increases to ~70%, which is enough to see the deviations. (Though still not actually spanning multiple orders of magnitude, where logarithmic really shines).