我试着在一个p上画两个函数

2024-04-29 15:06:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我到目前为止的代码,我正在尝试设置y极限为[0,4],x极限为[-2,3]。我可以自己处理情节标题,但我不知道如何在同一个图上得到这两个函数。你知道吗

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

Tags: 函数代码fromimport标题returnmatplotlibdef
3条回答

以下是我所做的,除了某些值的零除误差(我假设图形渐近线)外,它工作正常:

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()

当您需要更多引用时,这里有pyplot的文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html

这是一个模板,让你去

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()

要在同一个绘图上获得这些函数,只需使用plt.plot(...)两次。你知道吗

引用: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()

输出:

enter image description here

相关问题 更多 >