在 Python 中制作子图时调用其他函数

pli*_*erp 5 python plot loops matplotlib subplot

更新:我现在得到了正确位置的子图并显示了正确的数据!然而,还有一个问题,那就是我不能给子图自己的标题和颜色条。实际上,我宁愿在所有图的右侧只有一个颜色条,但这似乎也不起作用。这是我的新代码以及一些示例数据(非常没有洞察力,但仅用于测试目的):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cartopy.crs as ccrs

lons = np.arange(-180,180,1)
lats = np.arange(-80,80,1)
dens = np.zeros((12, len(lons), len(lats)))
for i in range(12):
   dens[i,:,:] = i

def plotTitle(yr):
   letter = chr(yr-2002+97)
   return '(' + letter + ') ' + str(yr)

def DensityPlot(yr, lon, lat, dens, ax):
   Lat, Lon = np.meshgrid(lat, lon)
   density = ax.pcolormesh(Lon, Lat, dens, cmap = 'jet')
   #cbar = ax.colorbar(density, orientation='vertical', shrink=0.5, extend='both') # this gives an error: ''GeoAxesSubplot' object has no attribute 'colorbar''
   ax.coastlines()
   plt.title(plotTitle(yr),fontsize=14,fontweight='bold') # this only works for the last plot. ax.title doesn't work, gives the error ''Text' object is not callable'

fig, axes = plt.subplots(nrows=6, ncols=2, figsize=(35,35), subplot_kw={'projection': ccrs.PlateCarree()}) ## check figsize
i=0
for ax in axes.flat:
   densdata = dens[i,:,:]
   density = DensityPlot(i+2002, lons, lats, densdata, ax)
   i=i+1
   #ax.title(plotTitle(i+2002)) # this gives the same error as mentioned before: ''Text' object is not callable'
#cbar = plt.colorbar(density, orientation='vertical', shrink=0.5, extend='both')
# this gives the following RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
fig.subplots_adjust(right=0.5)
Run Code Online (Sandbox Code Playgroud)

如何确保每个子图都有自己的标题,并且所有子图旁边都有一个颜色条?


我在 Python 中创建一组子图时遇到问题。我定义了一个产生特定图的函数:

def DensityPlot(lon, lat, dens):
   Lat, Lon = np.meshgrid(lat, lon)
   fig = plt.figure(figsize=(12,12))
   ax = plt.axes(projection=ccrs.PlateCarree())
   density = plt.pcolormesh(Lon, Lat, dens, cmap = 'jet')
   cbar = plt.colorbar(orientation='vertical', shrink=0.5, extend='both')
   cbar.set_label("Plastic Density ($10^{-3}$ particles km$^{-2}$)", rotation=90,fontsize=12)
   ax.coastlines()
   return density
Run Code Online (Sandbox Code Playgroud)

现在我想在 for 循环中使用这个函数来生成子图。为简单起见,我们假设我可以对lat,londens每个子图中使用相同的值(所以我只会多次获得相同的图)。但是,以下代码不起作用:

fig, axes = plt.subplots(nrows=6, ncols=2)
for j in range(0,12): 
   plt.subplot(6,2,j+1)
   density = DensityPlot(lon, lat, dens)
Run Code Online (Sandbox Code Playgroud)

发生以下事情:首先,显示 12 个空图,尽管它们的顺序正确(6x2)。之后,11 个空图彼此下方出现,最后最后一个图实际显示为应显示的方式。我已经检查了该函数的代码DensityPlot以创建单个图,并且知道它可以正常工作,因此问题必须出在子图创建中。这里可能有什么问题,我该如何解决?

Dav*_*idG 3

您自己怀疑它,您在循环之外创建子图,但每次调用函数时都会创建一个新图形。

您应该首先使用所需的投影创建图形和轴:

fig, axes = plt.subplots(nrows=6, ncols=2, subplot_kw={'projection': ccrs.PlateCarree()})
Run Code Online (Sandbox Code Playgroud)

然后,您可以循环遍历轴,并将其作为参数传递给函数(确保删除图形本身中的任何图形或轴创建):

def DensityPlot(lon, lat, dens, ax):
   Lat, Lon = np.meshgrid(lat, lon)
   density = ax.pcolormesh(Lon, Lat, dens, cmap = 'jet')
   cbar = ax.colorbar(orientation='vertical', shrink=0.5, extend='both')
   cbar.set_label("Plastic Density ($10^{-3}$ particles km$^{-2}$)", rotation=90,fontsize=12)
   ax.coastlines()

   return density

for ax in axes.flat:
    density = DensityPlot(lon, lat, dens, ax)
Run Code Online (Sandbox Code Playgroud)

为了向每个子图添加标题,您需要ax.set_title()在函数中使用:

ax.set_title(plotTitle(yr))
Run Code Online (Sandbox Code Playgroud)

关于为多个子图添加颜色条还有许多其他问题。例如Matplotlib 2 个子图,1 个颜色条