简体   繁体   中英

Python don't plot the graph

i have a little problem with matplotlib and python. So my problem is the line don't appear in the plot. I am trying to make a graph of a custom 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). Then, you want to create empty lists for your pmgc and pmec values to append to in your for loop. 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. But you've already defined np.linspace(2000, 32000) above in your code when you set x equal to it. So just do 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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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