如何调整Basemap散点图绘制顺序,让早期数据显示在顶层?
Hey there! The issue you're facing is super common with Matplotlib/Basemap: scatter points are drawn in the order they appear in your data array, so later groups (like Ports, E, D) end up covering earlier ones (A, B, C) because they're rendered on top. Here are two simple ways to fix this without rewriting most of your code:
Method 1: Reverse Your Data Arrays (Minimal Changes)
This is the quickest fix—just reverse your Xpos, Ypos, and color arrays so the groups you want on top (A, B, C) are the last ones drawn. Since Basemap/Matplotlib renders later elements on top, reversing the order will put your priority groups in front.
Modify your code like this:
# Keep your original data and color definitions exactly the same Xpos=[AX, BX, CX, DX, EX,SouXposJun,LonXposJun,ImmXposJun,LeiXposJun,FelXposJun,ZeelXposJun,RotXposJun,AmsXposJun] Ypos=[AY, BY, CY, DY, EY,SouYposJun,LonYposJun,ImmYposJun,LeiYposJun,FelYposJun,ZeelYposJun,RotYposJun,AmsYposJun] color=[] color[0:7500]=np.tile('red',7500)#A color[7500:92400]=np.tile('orange',84900)#B color[92400:288600]=np.tile('green',196200)#C color[288600:752400]=np.tile('darkred',463800)#D color[752400:775920]=np.tile('gold',23520)#E color[775920:800000]=np.tile('darkgrey',24080)#Ports # Add these three lines to reverse the arrays Xpos = Xpos[::-1] Ypos = Ypos[::-1] color = color[::-1] # Leave the rest of your plotting code unchanged ax=plt.figure(figsize=(20,10)) plt.subplot(231)#Start of release m= Basemap(width=1000000,height=1000500,projection='lcc', resolution='h',lat_0=52.25,lon_0=1) m.drawmapscale(lon=6.5, lat=48.5, lon0=0, lat0=48.5, length=50) m.drawmapboundary(fill_color='turquoise') m.fillcontinents(color='white',lake_color='aqua') m.drawcountries(linestyle='--') plt.title("17/02/2013",size=15) # Note: I noticed a possible typo here (XposFeb/YposJun) but kept your original code x,y = m(XposFeb[33],YposFeb[33]) scatter = m.scatter([], [], s=0.1, c=color, marker='o', label = 'Aurelia aurita', zorder=2) scatter.set_offsets(np.c_[x,y])
The [::-1] slice reverses the order of each array, so your A/B/C groups (originally first in the list) become the last elements to be drawn—meaning they'll sit on top of all other groups.
Method 2: Split Scatter Calls (More Control, Minor Refactoring)
If you want finer control over each group's layer position, split your scatter calls to draw lower-priority groups first, then higher-priority ones on top using the zorder parameter (higher values = closer to the viewer). This avoids reversing arrays and lets you tweak individual group styles easily.
Here's how to adjust your plotting section:
ax=plt.figure(figsize=(20,10)) plt.subplot(231)#Start of release m= Basemap(width=1000000,height=1000500,projection='lcc', resolution='h',lat_0=52.25,lon_0=1) m.drawmapscale(lon=6.5, lat=48.5, lon0=0, lat0=48.5, length=50) m.drawmapboundary(fill_color='turquoise') m.fillcontinents(color='white',lake_color='aqua') m.drawcountries(linestyle='--') plt.title("17/02/2013",size=15) # Draw lower-priority groups first (lower zorder) # Ports x_ports, y_ports = m(AmsXposJun, AmsYposJun) m.scatter(x_ports, y_ports, s=0.1, c='darkgrey', marker='o', zorder=2) # E Group x_e, y_e = m(EX, EY) m.scatter(x_e, y_e, s=0.1, c='gold', marker='o', zorder=3) # D Group x_d, y_d = m(DX, DY) m.scatter(x_d, y_d, s=0.1, c='darkred', marker='o', zorder=4) # Draw higher-priority groups on top (higher zorder) # C Group x_c, y_c = m(CX, CY) m.scatter(x_c, y_c, s=0.1, c='green', marker='o', zorder=5) # B Group x_b, y_b = m(BX, BY) m.scatter(x_b, y_b, s=0.1, c='orange', marker='o', zorder=6) # A Group (topmost) x_a, y_a = m(AX, AY) m.scatter(x_a, y_a, s=0.1, c='red', marker='o', label='Aurelia aurita', zorder=7)
This method gives you full control over which groups are on top, and you can adjust zorder values if you need to reorder groups later.
内容的提问来源于stack exchange,提问作者Jellyse

