简体   繁体   English

Python 不要 plot 图

[英]Python don't plot the graph

i have a little problem with matplotlib and python.我对 matplotlib 和 python 有一点问题。 So my problem is the line don't appear in the plot.所以我的问题是该行没有出现在 plot 中。 I am trying to make a graph of a custom function.我正在尝试制作自定义 function 的图表。 My code is here bellow:我的代码如下:

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

Everytime that i run the code the graph appears without the lines.每次我运行代码时,图表都会出现,没有线条。 Please could someone can help me?请问有人可以帮助我吗? Thank you for the attention:)感谢您的关注:)

You can create two lists that contain the info this way您可以通过这种方式创建两个包含信息的列表

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

this might not be the best way to do it, but it will work.这可能不是最好的方法,但它会起作用。

A few things.一些东西。 You are defining x as np.linspace(2000,32000) so use another variable in your for loop instead (such as i).您将x定义为np.linspace(2000,32000)因此在 for 循环中使用另一个变量(例如 i)。 Then, you want to create empty lists for your pmgc and pmec values to append to in your for loop.然后,您想在 for 循环中为您的 pmgc 和 pmec 值创建到 append 的空列表。 Lastly, you don't want to do for x in range(2000,32000): you want to do for i in np.linspace(2000, 32000): to match the length of your x list.最后,你不想for x in range(2000,32000):你想做for i in np.linspace(2000, 32000):来匹配你的 x 列表的长度。 But you've already defined np.linspace(2000, 32000) above in your code when you set x equal to it.但是当您设置x等于它时,您已经在代码中定义了上面的np.linspace(2000, 32000) So just do for i in x: .所以只需for i in x:做。 Put it all together, and you get your lines:把它们放在一起,你就会得到你的台词:

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

Output: Output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM