Python for(带zip)循环需要23小时才能执行

2024-10-04 07:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个Jupyter记事本,使用geopandas在地图(边界)中绘制特定纬度和经度的标记,但我有大约40000个位置,我需要根据条件用颜色标记(和着色)

地质勘探数据帧的屏幕截图如下:

enter image description here

代码片段:

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. GeoPandas Label Polygons
  2. https://www.geeksforgeeks.org/matplotlib-axes-axes-annotate-in-python/

Tags: inforaxlabeldictpatchcolorgeometry