简体   繁体   English

Matplotlib:绘制离散值

[英]Matplotlib: plotting discrete values

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 ? 有帮助吗?

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()

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

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