bhjghjh
bhjghjh

Reputation: 917

setting up ylim in python with matplotlib

I am trying to plot a simple graph with the following code:

import numpy as np
import matplotlib.pyplot as plt
import math

# evenly sampled time at 200ms intervals
t = np.arange(0., 60, .8)



plt.semilogy((((1- np.exp(-t**2/13.94))*(0.000567*(7.78*(1-np.exp(-33**2/t**2))*np.exp(-t/8.76)+1.50*(1-np.exp(-31**2/t**2))*(np.exp(-t/111.27)-np.exp(-t/8.76)))*10**43))+((1-np.exp(-t**2/13.94))*0.000567*0.05*(np.exp(-t/111.27)-np.exp(-t/8.76))*10**43)), linewidth=2)

#plt.legend([r"$t_{r} \approx (11 - 1)\sqrt{(M_{c}/M_{ch})}d$", r"$t_{r} \approx (11+1)\sqrt{(M_{c}/M_{ch})}d$"], loc='upper right')
plt.ylim(10**39, 10**41)
plt.xlabel("Time (Days)", fontsize =13)
plt.ylabel("Luminosity (erg/s)", fontsize=13)
plt.show()

the code works just fine if I comment out plt.ylim(10**39, 10**41) part. but without setting ylim, I have much white space in the plot which I don't want. the error I am receiving is this:

bolo.py:14: RuntimeWarning: divide by zero encountered in divide
  plt.semilogy((((1- np.exp(-t**2/13.94))*(0.000567*(7.78*(1-np.exp(-33**2/t**2))*np.exp(-t/8.76)+1.50*(1-np.exp(-31**2/t**2))*(np.exp(-t/111.27)-np.exp(-t/8.76)))*10**43))+((1-np.exp(-t**2/13.94))*0.000567*0.05*(np.exp(-t/111.27)-np.exp(-t/8.76))*10**43)), linewidth=2)
Traceback (most recent call last):
  File "bolo.py", line 17, in <module>
    plt.ylim(10**39, 10**41)
  File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1604, in ylim
    ret = ax.set_ylim(*args, **kwargs)
  File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 3047, in set_ylim
    bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
  File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/transforms.py", line 2751, in nonsingular
    if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I have no clue how to fix this. your suggestion is appreciated. I am also attaching the plot I have without ylim for now. enter image description here

Upvotes: 2

Views: 4640

Answers (1)

Longwen Ou
Longwen Ou

Reputation: 879

You should do it like this:

plt.ylim([float(10**39), float(10**41)])

The upper and lower limit should be passed in a list or tuple, not as separate arguments. Also it looks like your limit is too large that you need to manually convert it to float type.

Upvotes: 3

Related Questions