Python for (with zip) 循环执行需要 2-3 小时

Python for (with zip) loop takes 2-3 hours to execute

我正在使用 Jupyter Notebbok,使用 geopandas 在地图(边界)中的特定纬度和经度处绘制标记,但我有大约 40,000 个位置,我需要标记(和颜色)每个位置使用基于条件的颜色。

Geopandas 数据框 gdf 的屏幕截图如下:

代码段:

import matplotlib.patches as mpatches

# we have range of values from 0-15000
threshold1 = [8000,'#e60000']
threshold2 = [500,'#de791e']
threshold3 = [200,'#ff00ff']
threshold4 = [0   ,'#00ff0033']

# Create a dictionary of colors based on threshold
color_dict = {}
for x in gdf.n.to_list():
    if x>= threshold1[0]                    : color_dict[x] = threshold1[1]
    if x>= threshold2[0] and x<threshold1[0]: color_dict[x] = threshold2[1]
    if x>= threshold3[0] and x<threshold2[0]: color_dict[x] = threshold3[1] 
    if x<threshold3[0]                      : color_dict[x] = threshold4[1] 
        
        
# Set labels for the legend                
a_patch = mpatches.Patch(color = threshold1[1], 
                         label= str(threshold1[0]) + '-' + str(max(gdf.n.to_list())))

b_patch = mpatches.Patch(color = threshold2[1], 
                         label= str(threshold2[0]) + '-' + str(threshold1[0]))
                         
c_patch = mpatches.Patch(color = threshold3[1], 
                         label= str(threshold3[0]) + '-' + str(threshold2[0]))
                         
d_patch = mpatches.Patch(color = threshold4[1], 
                         label= str(min(gdf.n.to_list())) + '-' + str(threshold3[0]))





ax = gdf.plot(markersize=0 ,figsize = (20,20))
usa.geometry.boundary.plot(color=None,edgecolor='k',linewidth = 0.5, ax = ax)

# There are ~40,000 values to be iterated here
for x, y, label in zip(tqdm(gdf.geometry.x), gdf.geometry.y, gdf.n):
    ax.annotate('X', weight = 'bold', xy=(x, y), xytext=(x, y), fontsize= 8,color = color_dict[label], ha='center')
    sleep(0.1) 

ax.annotate(label, xy=(x, y), xytext=(x, y), fontsize= 8, color = color_dict[label], ha='center')

usa.apply(lambda x: ax.annotate(text = x.NAME, xy=x.geometry.centroid.coords[0], ha='center', fontsize= 2,color='black'),axis=1);


plt.xlim([-130,-60])
plt.ylim([20,55])


plt.legend(handles=[a_patch, b_patch, c_patch, d_patch])


plt.savefig("state.png",pad_inches=0, transparent=False, format = 'png')

我知道这条线花费的时间最多:

for x, y, label in zip(tqdm(gdf.geometry.x), gdf.geometry.y, gdf.n):
    ax.annotate('X', weight = 'bold', xy=(x, y), xytext=(x, y), fontsize= 8,color = color_dict[label], ha='center')
    sleep(0.1) 

但我想不出任何其他方法来标记每个坐标而不循环。请帮助我让它更快。 2-3小时对我的工作来说很不合理!谢谢!

我在代码中使用的一些参考资料:

  1. https://www.geeksforgeeks.org/matplotlib-axes-axes-annotate-in-python/

感谢@AirSquid——这是睡眠功能。天知道我怎么忽略了这一点。解决了。​​