Matplotlib: building discrete values

I am trying to build 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 chart (?). To make sure that the logic of the program is correct, I added the code print(x,y), just confirm that the pairs (x, y) are generated.

(x, y) pairs are generated, but there is no graph, I continue to receive an empty graph.

Any help?

+3
source share
1 answer

First of all, I sometimes had better success doing

from matplotlib import pyplot

instead of using pylab, although this should not matter in this case.

I think your actual problem may be that the points are plotted but not visible. It’s best to build all the points at once using the 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()

, :

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

Source: https://habr.com/ru/post/1740117/


All Articles