Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Logarithmic scale of the y-axis does not work when the range is small #29544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomManMaz opened this issue Jan 29, 2025 · 1 comment
Closed

Comments

@TomManMaz
Copy link

TomManMaz commented Jan 29, 2025

Bug summary

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

import random

import matplotlib.pyplot as plt


T = 3600
n = 1000
y_start = 10**4
y_end = 0.9*y_start

random.seed(42)
# Generate n increasing random values between 1 and T
x = [random.randrange(1, T) for _ in range(n)]
x.sort()
# Generate n decreasing random values between y_start and y_end
y = [random.uniform(y_end, y_start) for _ in x]

y.sort(reverse=True)

fig, ax = plt.subplots()
ax.set_yscale('log')
ax.plot(x, y)

plt.savefig('step_function.pdf')

Actual outcome

step_function.pdf

Expected outcome

I expected the logarithmis scale. In this example it happens if you set y_end = 0.2*y_start

step_function.pdf

Additional information

No response

Operating system

Linux debian 6.10.11+bpo-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.10.11-1~bpo12+1 (2024-10-03) x86_64 GNU/Linux

Matplotlib Version

3.10.0

Matplotlib Backend

qtagg

Python version

Python 3.13.0

Jupyter version

No response

Installation

conda

@ksunden
Copy link
Member

ksunden commented 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).

@ksunden ksunden closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants