1

I want to generate a line graph of temperature versus power and have written the following code:

import numpy as np 
import matplotlib.pyplot as plt

temps = np.arange(288, 314, 1)

sigma = 5.67e-8 #Stefan's constant, units = W m^-2 K^-4
T0 = 298 #ambient T, 25 degrees
emissivity = 0.96
A = (np.pi * 309)/80000 #total surface area of chameleon

def SB(T):
    power = A * sigma * emissivity * (T**4 - T0**4)
    return power

power = SB(temps)

plt.plot(temps, power)
plt.xlabel("Temperature (K)")
plt.ylabel("Power (W $m^{-2}$)")
plt.title("Net Power Radiated vs T")
plt.xlim(288, 314)
plt.ylim(0, 1.2)
plt.show()

The axes are showing but I'm not getting a line, could it be because the function doesn't take values from np.arange? If so how would I fix this? Any help would be appreciated! :D

Rachel

1 Answers1

0

This is a funny error. First thing, you don't see the line, because it's outside the ylim range. Delete the code line which sets the ylim, and you'll see the line.

However the line shows negative values - which is strange, since power should be positive and the formulas are correct.
The reason for that is that the input temperature array temps is an int32 array. This array to the power of four will give negative values, since the numbers are simply too big.

A solution to use a floating point array instead. Either use

temps = np.arange(288., 314., 1)

or

temps = np.arange(288, 314, 1).astype(np.float32)


This is reproduced with python 2.7 and numpy 1.12 on windows 8 64bit. The reason for this strange behaviour is seen in this question: "In Microsoft C, even on a 64 bit system, the size of the long int data type is 32 bits.".
Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712