-2

This is the code I have so far, I'm trying to set the y limit to be [0,4] and the x limit to be [-2,3]. I can take care of the plot titles myself but I can't figure out how to get these two functions on the same graph.

import math as m

from matplotlib import pylab as plt

import numpy as np

def fermi_dirac(x):

    fermi_result = (1/(np.exp(x)+1))

    return fermi_result

def bose_einstein(x):

    bose_result = (1/(np.exp(x)-1))

    return bose_result
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23
  • Possible duplicate of [How to plot multiple functions on the same figure, in Matplotlib?](https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure-in-matplotlib) – Banghua Zhao Nov 16 '18 at 01:06

4 Answers4

0

Here is a template to get you going

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

def fermi_dirac(x):

    fermi_result = (1./(np.exp(x)+1))

    return fermi_result

def bose_einstein(x):

    bose_result = (1/(np.exp(x)-1))

    return bose_result

x = np.linspace( -2,3, 100)
fd = fermi_dirac(x)
be = bose_einstein(x)

plt.figure()
plt.plot(x, fd, label='fermi dirac')
plt.plot(x, be, label ='bose einstein')
plt.legend(loc='best')
plt.show()
James Fulton
  • 322
  • 2
  • 8
0

Here's what I did and it works fine with the exception of a divide by zero error for certain values (I'm assuming graphical asymptotes):

import matplotlib.pyplot as plt
import numpy as np

def fermi_dirac(x):
    fermi_result = (1/(np.exp(x)+1))
    return fermi_result

def bose_einstein(x):
    bose_result = (1/(np.exp(x)-1))
    return bose_result

f = plt.figure()

x_vals = range(-2,3)

plt.plot(x_vals, fermi_dirac(x_vals))
plt.plot(x_vals, bose_einstein(x_vals))
plt.show()

Here's the documentation for pyplot when you need more references: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html

A. Hudson
  • 51
  • 1
  • 10
0

To get those functions on the same plot, just use plt.plot(...) two times.

Reference: How to plot multiple functions on the same figure, in Matplotlib?

import math as m

from matplotlib import pylab as plt

import numpy as np

def fermi_dirac(x):
    fermi_result = (1/(np.exp(x)+1))
    return fermi_result

def bose_einstein(x):
    bose_result = (1/(np.exp(x)-1))
    return bose_result

x = np.linspace(-2, 3, 100)
y1 = fermi_dirac(x)
y2 = bose_einstein(x)

plt.plot(x, y1, 'r')
plt.plot(x, y2, 'b')
plt.ylim(0, 4) 
plt.show()

Output:

enter image description here

Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23
  • Ty, this is closer to the result I was looking for, the assignment was to replicate the plot given in our book in python, normally I use gnuplot for stuff like this but he specifically asked for python on this assignment. – user10525863 Nov 16 '18 at 01:11
0

Very simple, you just have to define an array of input values (that you can call x). Here's an example with 1000 such values, input as a line plot using both formulas and the axis ranges you provided:

x = np.linspace(-2, 3, 1000)
plt.xlim([-2, 3])
plt.ylim([0,4])
plt.plot(x, fermi_dirac(x), '-', x, bose_einstein(x), '--')
plt.show()

enter image description here

AlecZ
  • 546
  • 5
  • 9