我正在尝试在一个绘图上绘制两个函数

时间:2018-11-16 00:47:54

标签: python plot

这是我到目前为止的代码,我试图将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

4 个答案:

答案 0 :(得分:0)

这是让您前进的模板

function printAnyRange(rangeStart, rangeStop) {

    if (rangeStart < rangeStop) {
        return printRange(rangeStart, rangeStop);
    } else {
        return printRangeReversed(rangeStart, rangeStop);
    }
}
console.log(printAnyRange(10, 15));

答案 1 :(得分:0)

这就是我的工作,除了某些值的零除误差(我假设是图形渐近线)之外,它工作正常,

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

答案 2 :(得分:0)

要在同一图中获得这些函数,只需使用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

答案 3 :(得分:0)

非常简单,您只需定义一个输入值数组(可以称为x)。这是一个具有1000个此类值的示例,使用公式和您提供的轴范围作为折线图输入:

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

相关问题