Fra*_*Vel 1 python plot matplotlib
在 pyplot 中绘制 y 值很容易,给定一个列表y_values = [0, 1, 4, 9]
,pyplot 自动使用
plt.plot(y_values)
plt.show()
Run Code Online (Sandbox Code Playgroud)
由于 pyplot 使用[0,1,2,3]
. 但是,给定 的列表x_values
,有没有办法在不提供 y 值的情况下自动绘制这些?例如让 pyplot 自动枚举它们?
我试过了
plt.plot(x=x_values); plt.plot(xdata=x_values)
Run Code Online (Sandbox Code Playgroud)
然而,这些似乎都不起作用。当然,一种方法是翻转轴,但是我忽略了一种更简单的方法吗?
中的 x 和 y 参数pyplot.plot(*args, **kwargs)
是位置参数。根据文档,例如
plot(x, y) # plot x and y using default line style and color
plot(x, y, 'bo') # plot x and y using blue circle markers
plot(y) # plot y using x as index array 0..N-1
Run Code Online (Sandbox Code Playgroud)
现在,pyplot 怎么知道如果你指定一个参数,你会希望它被解释为纵坐标而不是坐标?函数的编写方式根本不可能。
针对某个列表绘制索引的解决方案是提供索引作为y
参数:
import matplotlib.pyplot as plt
x_values = [0, 1, 4, 9]
plt.plot(x_values, range(len(x_values)))
plt.show()
Run Code Online (Sandbox Code Playgroud)