[英]Python don't plot the graph
我对 matplotlib 和 python 有一点问题。 所以我的问题是该行没有出现在 plot 中。 我正在尝试制作自定义 function 的图表。 我的代码如下:
fig, ax = plt.subplots(figsize=(8,4))
# Define the x axis values:
x = np.linspace(2000,32000)
# Creating the functions that we will plot
def pmgc(x):
return 0.853
def pmec(x):
return (-124.84/(x)) + pmgc(x)
for x in range(2000,32000):
pmgc(x)
pmec(x)
#Plotting
ax.plot(x,pmgc(x), color = 'blue',linewidth = 3)
ax.plot(x,pmec(x), color = 'red',linewidth = 3)
plt.rcParams["figure.autolayout"] = True
ax.set_xlabel("Renda")
plt.legend(labels = ['Propensão Marginal a Cosumir','Propensão Média a Cosumir'],loc = 'upper left', borderaxespad = 0,bbox_to_anchor=(1.02, 1))
plt.title('Gráfico da Questão 6, item c\nFeito por Luiz Mario. Fonte: Autor', loc='center')
每次我运行代码时,图表都会出现,没有线条。 请问有人可以帮助我吗? 感谢您的关注:)
您可以通过这种方式创建两个包含信息的列表
# Define the x axis values:
x = np.linspace(2000,32000)
# Creating the functions that we will plot
# create three empty sets
x_list = []
y_list1 = []
y_list2 = []
def pmgc(x):
return 0.853
def pmec(x):
return (-124.84/(x)) + pmgc(x)
for x in range(2000,32000):
# fill in the sets
x_list.append(x)
y_list1.append(pmgc(x))
y_list2.append(pmec(x))
#Plotting
# add x_list and y_list respectively
ax.plot(x_list,y_list1, color = 'blue',linewidth = 3)
ax.plot(x_list,y_list2, color = 'red',linewidth = 3)
plt.rcParams["figure.autolayout"] = True
ax.set_xlabel("Renda")
plt.legend(labels = ['Propensão Marginal a Cosumir','Propensão Média a Cosumir'],loc = 'upper left', borderaxespad = 0,bbox_to_anchor=(1.02, 1))
plt.title('Gráfico da Questão 6, item c\nFeito por Luiz Mario. Fonte: Autor', loc='center')
plt.show()
这可能不是最好的方法,但它会起作用。
一些东西。 您将x
定义为np.linspace(2000,32000)
因此在 for 循环中使用另一个变量(例如 i)。 然后,您想在 for 循环中为您的 pmgc 和 pmec 值创建到 append 的空列表。 最后,你不想for x in range(2000,32000):
你想做for i in np.linspace(2000, 32000):
来匹配你的 x 列表的长度。 但是当您设置x
等于它时,您已经在代码中定义了上面的np.linspace(2000, 32000)
。 所以只需for i in x:
做。 把它们放在一起,你就会得到你的台词:
fig, ax = plt.subplots(figsize=(8,4))
# Define the x axis values:
x = np.linspace(2000,32000)
# Creating the functions that we will plot
def pmgc(x):
return 0.853
def pmec(x):
return (-124.84/(x)) + pmgc(x)
pmgc_list = []
pmec_list = []
for i in x:
pmgc_list.append(pmgc(i))
pmec_list.append(pmec(i))
#Plotting
ax.plot(x,pmgc_list, color = 'blue',linewidth = 3)
ax.plot(x,pmec_list, color = 'red',linewidth = 3)
plt.rcParams["figure.autolayout"] = True
ax.set_xlabel("Renda")
plt.legend(labels = ['Propensão Marginal a Cosumir','Propensão Média a Cosumir'],loc = 'upper left', borderaxespad = 0,bbox_to_anchor=(1.02, 1))
plt.title('Gráfico da Questão 6, item c\nFeito por Luiz Mario. Fonte: Autor', loc='center')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.