简体   繁体   English

在pyplot中绘制没有y值的x值

[英]Plot x-values without y-values in pyplot

Plotting y-values in pyplot is easy, given a list of y_values = [0, 1, 4, 9] , pyplot automatically plots this using 给定y_values = [0, 1, 4, 9]的列表,在pyplot中绘制y值很容易,pyplot使用以下命令自动绘制该值

plt.plot(y_values)
plt.show()

As pyplot automatically enumerates these using [0,1,2,3] . 因为pyplot使用[0,1,2,3]自动枚举了这些。 However, given a list of x_values , is there a way to automatically plot these without providing y-values? 但是,给定一个x_values列表,是否有一种方法可以自动绘制这些图而无需提供y值? eg let pyplot automatically enumerating them? 例如让pyplot自动枚举它们?

I've tried 我试过了

plt.plot(x=x_values); plt.plot(xdata=x_values)

However none of these seem to work. 但是这些似乎都不起作用。 Of course, one way would be to flip the axes, but is there a simpler way I've overlooked? 当然,一种方法是翻转轴,但是有没有我忽略的简单方法?

The x and y arguments in pyplot.plot(*args, **kwargs) are positional arguments. pyplot.plot(*args, **kwargs)中的x和y参数是位置参数。 According to the documentation, eg 根据文档,例如

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

Now, how would pyplot know that if you specify a single argument, you would want it to be interpreted as the ordinate instead of the coordinate? 现在,pyplot如何知道如果您指定一个参数,您希望将其解释为纵坐标而不是坐标? It's simply not possible the way the function is written. 函数编写的方式根本不可能。

A solution to plot the index against some list is to supply the index as y argument: 一种针对某些列表绘制索引的解决方案是将索引作为y参数提供:

import matplotlib.pyplot as plt
x_values = [0, 1, 4, 9]
plt.plot(x_values, range(len(x_values)))
plt.show()

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

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