简体   繁体   English

尝试使用 plt.plot() plot 没有成功

[英]Trying to plot with plt.plot() without success

I am trying to plot a graph in python and I have defined all x-and y-values in lst_x and lst_y (made a previous question about that):我正在尝试 plot python 中的图表,并且我已经在lst_xlst_y中定义了所有 x 和 y 值(对此提出了先前的问题):

import matplotlib.pyplot as plt

def plot_poly(p,x_start=-10,x_end=10,color='b'):

    lst_x = list(range(x_start, x_end + 1))
    print(lst_x)

    lst_y = [eval_poly(p,x) for x in lst_x]
    print(lst_y)

    return plt.plot(lst_y, lst_x, 'b')
    plt.show()

    # eval_poly is another function which produces all y-values for all x:
    
    # def eval_poly(p,x): 
        # ans = 0
        # for degree,coeff in enumerate(p):
        # ans += coeff*x**degree # ekvationen som ska räknas
        # return ans

But when trying to perform this I only get:但是当尝试执行此操作时,我只会得到:

[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[102, 83, 66, 51, 38, 27, 18, 11, 6, 3, 2, 3, 6, 11, 18, 27, 38, 51, 66, 83, 102]
Out[265]: [<matplotlib.lines.Line2D at 0x126e4e0d0>]

The printed lists have the correct values, but it doesn't plot.打印的列表具有正确的值,但不是 plot。 and I don't understand why because I have plt.show() which otherwise is usually the reason why it does not work?我不明白为什么,因为我有plt.show()否则通常是它不起作用的原因?

The return statement terminate the function without calling the show function: return 语句终止 function 而不调用 show function:

import matplotlib.pyplot as plt导入 matplotlib.pyplot 作为 plt

def plot_poly(p,x_start=-10,x_end=10,color='b'):

    lst_x = list(range(x_start, x_end + 1))
    print(lst_x)

    lst_y = [eval_poly(p,x) for x in lst_x]
    print(lst_y)

    plt.plot(lst_y, lst_x, 'b')
    return plt.show()

Remove the return in the function去掉 function 中的返回

import matplotlib.pyplot as plt


def plot_poly(p,x_start=-10,x_end=10,color='b'):

    lst_x = list(range(x_start, x_end + 1))
    print(lst_x)

    lst_y = [eval_poly(p,x) for x in lst_x]
    print(lst_y)

    plt.plot(lst_y, lst_x, 'b')
    plt.show()

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

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