Axes scale won't change using matplotlib
I am trying to plot with matplotlib with the following x values:
[1.1e19, 5.6e31, 1.1e32, 1.6e32, 2.2e32, 2.8e32]
However, since my first x value is so small compared to the others, matplotlib gives me the following autoscaled x-axis . My first point is on the y-axis. I would like this moment to be more prominent.
I tried:
ax.set_xlim(1e16, 1e36) ax.set_xlim(xmin=1e16, xmax=1e36) ax.set_xbound(lower=1e16, upper=1e36)
to no avail. I can change the top x-axis but never the bottom.
Edit: For anyone asking for sample 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()
source to share
Good news, bad news ...
Your code actually works. if you look at the values ax.get_xlim()
, you will see the bounds you set.
(Using Spyder / Ipython for some quick tests)
The next two screenshots are your graph ... the linear axis and the log axis.
So it looks like matplotlib has a limit on the size / scale of the range that you can view with a linear axis. All data less than three or four orders of magnitude smaller than your upper bound is compressed so that it looks correct on the y-axis
The bad news is that I cannot provide a real solution to this problem ... simply because you are not doing anything wrong. Could it be a mistake? I would suggest opening a question on the matplotlib github page to see if there is a more complex solution, or if one of the developers has an idea.
** Note: not a bug ... but leaving a link to the dev team anyway
after @PaulH, if your upper limit is 1e32 then: 1e31 is plotted at 0.1 1e30 is plotted at 0.01 1e29 is mapped to 0.001 | ...
1e16 is 0.0000000000000001
no matter what your lower bound is.
so this is not an error, you always put this point imperceptibly near the y-axis, even if the lower bound is 1e16 ...
source to share
Avoiding setting limits 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()
outputs this figure:
which looks good to me.
Of course, you can change the axis limits if you like. For example. using
ax.set_xlim(xmin=-1e32, xmax=5e32)
will lead to
source to share