2017-02-12 1 views
0

Ich habe ein Programm erstellt, das Primzahlen ausarbeitet. Es war nur ein Test, aber ich dachte, dass es interessant wäre, das dann auf einem Graphen darzustellen.Wie zeichne ich eine Liste auf einmal mit matplotlib.pyplot?

import matplotlib.pyplot as plt 

max = int(input("What is your max no?: ")) 
primeList = [] 

for x in range(2, max + 1): 
    isPrime = True 
    for y in range (2 , int(x ** 0.5) + 1): 
    if x % y == 0: 
     isPrime = False 
     break 

    if isPrime: 
     primeList.append(x) 


print(primeList) 

Wie kann ich dann plotten primeList, kann ich es außerhalb der for-Schleife auf einmal?

+0

Derzeit sind Sie nicht etwas Plotten? Außerdem, was würdest du auf deiner 'x-Achse' anspielen? Sie müssen nur x/y-Paare in Listen gleicher Länge liefern. – roganjosh

+0

'plt.plot (Bereich (len (primeList)), primeList)' zum Beispiel. – roganjosh

+0

Ich hatte einen zusätzlichen Block von Code, der das geplottet und eine Variable namens itemsInList, die die Menge der Elemente in primeList war –

Antwort

1

Dies zeichnet Ihre Liste gegen seinen Index:

from matplotlib import pyplot as plt 

plt.plot(primeList, 'o') 
plt.show() 

Dieses Programm:

from matplotlib import pyplot as plt 

max_= 100 
primeList = [] 

for x in range(2, max_ + 1): 
    isPrime = True 
    for y in range (2, int(x ** 0.5) + 1): 
     if x % y == 0: 
      isPrime = False 
      break 
    if isPrime: 
     primeList.append(x) 

plt.plot(primeList, 'o') 
plt.show() 

dieses Grundstück ergibt:

enter image description here

Verwandte Themen