[英]matplotlib scatter takes too long
Consider the following code which produces a random sequence of dots of various colours.考虑下面的代码,它产生各种颜色的点的随机序列。
import matplotlib.pyplot as plt
import random
n = 100
x = [0]*n
y = [0]*n
col=['red']*n
for i in range(1,n):
val = random.randint(1,2)
if val == 1:
col[i] = 'red'
if val == 2:
col[i] = 'yellow'
ax=plt.figure().add_subplot()
for i in range(n):
ax.scatter(i,0, color=col[i])
#print(x[i],y[i],col[i])
plt.show()
Right now it does 100 point, however I would like it to do 10000 points.现在它做 100 点,但我希望它做 10000 点。 Doing this takes too long however, how can I make this faster?
但是,这样做需要很长时间,我怎样才能使它更快? Thanks
谢谢
Avoid the unnecessary for loop for plotting n
scatter points one at a time.避免不必要的 for 循环一次绘制
n
散点。 Instead, plot the whole range at once and pass the whole col
list for colors相反,一次绘制整个范围并传递整个
col
列表的颜色
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(range(n), [0]*n, color=col)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.