Matplotlib:绘制离散值

发布于 2024-08-27 18:50:56 字数 334 浏览 10 评论 0原文

我正在尝试绘制以下内容!

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

但是,我不断得到一个空白图表(?)。为了确保程序逻辑正确,我添加了代码 print(x,y),只是确认正在生成 (x,y) 对。

正在生成 (x,y) 对,但没有绘图,我一直得到空白图。

有什么帮助吗?

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.

(x,y) pairs are being generated, but there is no plot, I keep getting a blank graph.

Any help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2024-09-03 18:50:56

首先,有时我通过这样做

from matplotlib import pyplot

而不是使用 pylab 取得了更好的成功,尽管在这种情况下这应该不会产生影响。

我认为您的实际问题可能是正在绘制点但不可见。使用列表一次绘制所有点可能会更好:

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

为了使这更简洁,您可以使用生成器表达式:

xPoints = range(1,500)
yPoints = [random.randint(1,25000) for _ in range(1,500)]
pyplot.plot(xPoints, yPoints)
pyplot.show()

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.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文