Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axis scale will not change with matplotlib

I'm trying to plot with matplotlib with the following x-values:

[1.1e19, 5.6e31, 1.1e32, 1.6e32, 2.2e32, 2.8e32]

However, because my first x-value is so small relative to the others, matplotlib gives me the following autoscaled x-axis. My first point lies on the y-axis. I'd like for this point to be more visible.

I've tried:

ax.set_xlim(1e16, 1e36)
ax.set_xlim(xmin=1e16, xmax=1e36)
ax.set_xbound(lower=1e16, upper=1e36)

to no avail. I am able to change the upper bound of my x-axis, but never the lower bound.

Edit: For those asking for example code, this is what I'm running:

dose = np.array([1.1e19, 5.6e31, 1.1e32, 1.6e32, 2.2e32, 2.8e32])
counts = np.array([46, 31, 15, 16, 14, 13])

plt.rcParams['axes.formatter.useoffset'] = False 
fig, ax = plt.subplots()
ax.plot(dose, counts, 'ob-')
ax.set_xlim(1e16, 1e36)
#ax.set_xlim(xmin=1e16, xmax=1e36)
#ax.set_xbound(lower=1e16, upper=1e36)

fig.tight_layout()
plt.show()
like image 282
Maggie Rivers Avatar asked Jan 23 '26 14:01

Maggie Rivers


2 Answers

Good news, Bad news...

Your code actually is working. if you look at the values ax.get_xlim() you will see the boundaries you set.

(Using Spyder/Ipython for some quick tests)

The following two screenshots are your plot... a linear axis and a log axis. linear example log example

So it looks like matplotlib has a limit to the size/scale of the range you can view using a linear axis. All data less then three or four orders of magnitude smaller than your upper boundary get compressed so that look to be right on the y-axis

Bad news is that I cannot provide an actual solution to this challenge... just that you aren't doing anything incorrect. Maybe its a bug? I would suggest opening an issue on the matplotlib github page to see if there is a more complex solution or one of the developers has an idea.

**Note: Not a bug... but leaving link to development team anyways

following @PaulH if your upper limit is 1e32 then: 1e31 gets plotted at 0.1
1e30 gets plotted at 0.01
1e29 gets plotted at 0.001
...
1e16 gets plotted at 0.0000000000000001

independent of what your lower boundary is.

so it isn't a bug, you will always be putting that point imperceptibly near the y-axis even if the lower bound is 1e16...

like image 80
Shaun Avatar answered Jan 26 '26 07:01

Shaun


Ommiting the limits setting would be an option. The following code

import matplotlib.pyplot as plt
import numpy as np

dose = np.array([1.1e19, 5.6e31, 1.1e32, 1.6e32, 2.2e32, 2.8e32])
counts = np.array([46, 31, 15, 16, 14, 13])

fig, ax = plt.subplots()
ax.plot(dose, counts, 'ob-')

fig.tight_layout()
plt.show()

produces this figure:

enter image description here

which looks fine to me.

Of course you can change the axis limits, if you wish to. E.g. using

ax.set_xlim(xmin=-1e32, xmax=5e32)

would result in

enter image description here

like image 25
ImportanceOfBeingErnest Avatar answered Jan 26 '26 07:01

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!