Python 尝试使用plt.plot()绘图但未成功
我正试图用python绘制一个图,我已经在Python 尝试使用plt.plot()绘图但未成功,python,matplotlib,Python,Matplotlib,我正试图用python绘制一个图,我已经在lst_x和lst_y中定义了所有的x和y值(提出了前面的问题): 但在尝试执行此操作时,我只得到: [-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]: [<matpl
lst_x
和lst_y
中定义了所有的x和y值(提出了前面的问题):
但在尝试执行此操作时,我只得到:
[-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>]
[-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]
出[265]:[]
打印的列表具有正确的值,但不会打印。我不明白为什么,因为我有
plt.show()
,否则这通常是它不工作的原因?返回语句在不调用show函数的情况下终止函数:
将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()
return语句在不调用show函数的情况下终止函数: 将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()
删除函数中的返回
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()
删除函数中的返回
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()
您使用哪种IDE?请修复代码上的缩进,这不容易理解。谢谢plt.show()在函数之外吗?我猜您在plt.show()下面的某个地方调用了该函数?@Guimoute我使用Python@TomMyddeltyn我的道歉,这已经解决了。谢谢您使用哪种IDE?请修复代码上的缩进,这不容易理解。谢谢plt.show()在函数之外吗?我猜您在plt.show()下面的某个地方调用了该函数?@Guimoute我使用Python@TomMyddeltyn我的道歉,这已经解决了。谢谢您的打印数据是向后的。您的打印数据是向后的。这最终起作用了,返回可能是个大问题!非常感谢。这最终奏效了,回归可能是个大问题!非常感谢。