首页 > 解决方案 > 将散点图作为熊猫数据框中的子图

问题描述

我有一个包含值、纬度、经度和年份变量的分组数据框。我想在地图上每年绘制不同位置的值。

This is how my dataframe looks:
from mpl_toolkits.basemap import Basemap

dtype: object
          lat        lon                      Date       mean  Year
0   2.912811  7.609220 2017-12-31 00:00:00+05:30  27.146307  2017
1   2.938906  7.697270 2017-12-31 00:00:00+05:30  19.067468  2016

这就是我在 4 个子图中绘制 2016 年至 2019 年数据的方法:df_yearly.dropna()

#Data to be shown on the map:
#fig = plt.figure(figsize=(6.5,9.0))
fig_title = 'Combined_map.pdf'
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))


year_start=2016
year_stop=2019

for i in range (1,5):

  #for year in range(year_start, year_stop+1):
    for year in df_yearly['Year']:
      #print (year)
      data = np.array(conc)
      #create figure and axes instances                                                                                                                               
      plot_location = '22'+ str(i)
      print ('plot_location: '), 
      print (plot_location)

     #plt.subplot(plot_location)
      plt.tight_layout()


    #create polar stereographic Basemap instance                                                                                                                    
      m = Basemap(projection='mill',llcrnrlat=0,urcrnrlat=60,llcrnrlon=70,urcrnrlon=11, resolution='c')

# draw coastlines, state and country boundaries, edge of map.                                                                                                   
      m.drawcoastlines(linewidth =1.5, color ="black")
      m.drawcountries(linewidth =1.5, color ="black")

      m.drawmapboundary()

 #Scatter:
      m.scatter(lon, lat, latlon=True,c=np.array(conc),
              cmap='jet', alpha=2)


# 3. create colorbar and legend
      plt.colorbar(label=r'$Concentration$')
      plt.clim(0, 200)
      ptitle = "concentrations: "+str(year)     
      plt.title(ptitle, fontsize=7, weight='bold')

#plt.show()
plt.savefig(fig_title,bbox_inches="tight")        

print ('Plotted:'), (fig_title)    

如何将不同年份的值绘制在不同的散点图子图中?

标签: pandasmatplotlib-basemapsubplot

解决方案


推荐阅读