Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

Any help ?

like image 832
Arkapravo Avatar asked Oct 15 '22 07:10

Arkapravo


1 Answers

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()
like image 174
Daniel G Avatar answered Oct 19 '22 13:10

Daniel G