簡體   English   中英

Python 不要 plot 圖

[英]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')

Output: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM