为什么即使定义了变量,我的图也没有显示

时间:2020-06-24 18:14:42

标签: python numpy matplotlib

因此,我编写了这段代码以创建一个看起来像这样的图[此图像是在Mathematica中完成的] 1,但是由于某种原因,在图plot i made上没有任何显示。它必须与gam(x_2)或gam本身有关的东西,因为我尝试将其定义为范围,但仍然一无所获。请教我。从matematica中绘制的图上,似乎他将x和y的范围都设置到了10,000。

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

%matplotlib inline


gam0 = 72.8
temp = 293.15
def gam(x_2):
return gam0 - 0.0187 * temp * math.log10(1+628.14*55.556*x_2)

x = range(0, 10000)
x_2= x



plt.plot('gam(x_2), x_2')

plt.xlabel('Log_10x_2')
plt.ylabel('gamma (erg cm^2)')

2 个答案:

答案 0 :(得分:0)

需要一些修复;定义函数时,会出现缩进,并且整个数组与'*'的运算将无法正常工作,因此您可以通过for循环将值保存在单独的数组中:

编辑:哦,在绘制时,您也不要将变量名放在字符串中,而是直接按原样调用它们。

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

%matplotlib inline


gam0 = 72.8
temp = 293.15

x = range(0, 10000)
x_2= x

def gam(x_2):
    returns = []
    for x_i in x_2:
        returns.append(gam0 - 0.0187 * temp * math.log10(1+628.14*55.556*x_i))
    return returns

plt.plot(gam(x_2), x_2)

plt.xlabel('Log_10x_2')
plt.ylabel('gamma (erg cm^2)')
plt.show()

答案 1 :(得分:0)

缩进功能

def gam(x_2):
    return gam0 - 0.0187 * temp * math.log10(1+628.14*55.556*x_2)

为列表gam(x_2)中的每个项目(x_2)查找x

gam_x = [gam(x_2) for x_2 in x]

最后,绘制并显示。

plt.plot(gam_x, x)

plt.xlabel('Log_10x_2')
plt.ylabel('gamma (erg cm^2)')
plt.show()