3

I am trying to plot some extremely small values with matplotlib in jupyter notebook (on a macbook pro). However, regardless if I set the y-axis limits, all I get is a flat line. What I am after is something like the example (png) below with regard to y-axis notation. I also tried the same example outside of jupyter and I still get the same results. Here's the code suggested by Andrew Walker on my previous question:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)


xs = np.linspace(0, 1, 101)
ys = 1e-300 * np.exp(-(xs-0.5)**2/0.01)

ax.plot(xs, ys, marker='.')

Here's what I get:

enter image description here

And here's what I'm after:

enter image description here

Community
  • 1
  • 1
spyrostheodoridis
  • 508
  • 2
  • 8
  • 27
  • 1
    Having tested it `ax.set_ylim(0, 1e300)` actually sets it to `-0.001` (it returns the limit when setting). The lowest you can go and actually have it work is `ax.set_ylim(0.0,1e-286)` – mfitzp Oct 30 '16 at 13:45
  • `1e-300` is close to the minimum you can represent in a double. Better to scale your data so you're not in that range. – poolie Oct 30 '16 at 13:56

2 Answers2

3

The easiest thing to do is to just plot your values multiplied by 10^300, and then change the y-axis label:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)

xs = np.linspace(0, 1, 101)
ys = np.exp(-(xs-0.5)**2/0.01)

ax.plot(xs, ys, marker='.')

ax.set_ylabel(r'Value [x 10^{-300}]')
0

You can use the set_ylim method on your axes object to do what you need, simply change your code to this and it would do what you need:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)


xs = np.linspace(0, 1, 101)
ys = 1e-300 * np.exp(-(xs-0.5)**2/0.01)

ax.set_ylim([0,10^-299])

ax.plot(xs, ys, marker='.')

you may like to check This link for more info on this subject.

Community
  • 1
  • 1
Sina Mansour L.
  • 418
  • 4
  • 8
  • 1
    This doesn't work (try it) — the 1e-299 is too small and matplotlib doesn't accept it as a limit value.. – mfitzp Oct 30 '16 at 13:47