[英]How do I add a second plot behind the first one in matplotlib.pyplot? It's either on top or invisible
[英]How do I plot a list all at once with 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)
然后如何绘制primeList
,我可以在for循环之外一次完成所有操作吗?
这将绘制您的列表及其索引:
from matplotlib import pyplot as plt
plt.plot(primeList, 'o')
plt.show()
该程序:
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()
产生此图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.