python Matplotlib:绘制离散值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2590768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib: plotting discrete values
提问by Arkapravo
I am trying to plot the following !
我正在尝试绘制以下内容!
from numpy import *
from pylab import *
import random
for x in range(1,500):
y = random.randint(1,25000)
print(x,y)
plot(x,y)
show()
However, I keep getting a blank graph (?). Just to make sure that the program logic is correct I added the code print(x,y)
, just the confirm that (x,y) pairs are being generated.
但是,我一直得到一个空白图表(?)。为了确保程序逻辑正确,我添加了代码print(x,y)
,只是确认正在生成 (x,y) 对。
(x,y) pairs are being generated, but there is no plot, I keep getting a blank graph.
(x,y) 对正在生成,但没有情节,我一直得到一个空白图。
Any help ?
有什么帮助吗?
采纳答案by Daniel G
First of all, I have sometimes had better success by doing
首先,我有时通过这样做获得了更好的成功
from matplotlib import pyplot
instead of using pylab, although this shouldn't make a difference in this case.
而不是使用 pylab,尽管在这种情况下这应该没有区别。
I think your actual issue might be that points are being plotted but aren't visible. It may work better to plot all points at once by using a list:
我认为您的实际问题可能是正在绘制点但不可见。使用列表一次绘制所有点可能会更好:
xPoints = []
yPoints = []
for x in range(1,500):
y = random.randint(1,25000)
xPoints.append(x)
yPoints.append(y)
pyplot.plot(xPoints, yPoints)
pyplot.show()
To make this even neater, you can use generator expressions:
为了使这更整洁,您可以使用生成器表达式:
xPoints = range(1,500)
yPoints = [random.randint(1,25000) for _ in range(1,500)]
pyplot.plot(xPoints, yPoints)
pyplot.show()