Python:摄影和pcoLormesh,做一个循环

发布于 2025-01-31 09:16:24 字数 1428 浏览 4 评论 0原文

我实际上是实习,我想在几个月中绘制地图的海面数据的一定温度(我想要50年,但我稍后会看到),我希望它专注于精确的区域。我已经成功地做到了这一点,除了在每个地图中绘制数据时(每月12张地图)。我每月都有相同的数据,似乎一月的温度在每个地图中。这条线可能是一个问题:

ax.pcolormesh(lon, lat, tos, data=dset, cmap=cmap)

但是我不知道如何修复它。也许我必须在此代码行中进行其他循环,但是有可能吗?感谢您的回答!

代码:

from netCDF4 import Dataset
import matplotlib.pylab as plt
import cartopy.crs as ccrs
import numpy as np
import netCDF4 as nc

month_name=["Jan","Feb","Mar","Apr","May","June","Jul","Aug","Sep","Oct","Nov","Dec"]

data = "/Users/name/Desktop/Internship/tos_1979.nc" 
dset = Dataset(data)
fig = plt.figure(figsize=(24,24))
cmap = plt.cm.jet
for imonth in np.arange(1,13):  
ax = fig.add_subplot(4,3,imonth, projection=ccrs.PlateCarree())   
  tos = dset.variables['tos'][0, :, :]
  lon = dset.variables['longitude'][:]
  lat = dset.variables['latitude'][:]
  time = dset.variables['time'][:]

  ax.set_extent([-70,20,30,90])
  ax.coastlines();
  plt.title('Ocean Surface Temperature :'+ month_name[imonth-1],fontsize=12
  ax.stock_img()
  ax.gridlines()
  ax.pcolormesh(lon, lat, tos, data=dset, cmap=cmap)
plt.savefig('fig_tos_1979.png')
plt.show()

只是要知道,我在MacBook上(带Spyder)与Python代码。

另外,我对ScaleBar工具有问题,它已安装在我的Mac上(感谢终端),但是当我导入此工具时,它说:

  ImportError: cannot import name 'scale_bar' from 'scalebar' (/Users/name/opt/anaconda3/lib/python3.9/site-packages/scalebar/__init__.py)

谢谢!

I an actually in internship, and I want to plot some temperature of the ocean surface data of a map for each months for on years (I want for 50 years but I will see later) and I want it to be focus on a precise area. I have succeed to do this except that when I plot my data, in each maps (12 maps for each months). I have the same data on every months, it seems that the temperature for January are in each maps. It is probably a problem with this line:

ax.pcolormesh(lon, lat, tos, data=dset, cmap=cmap)

But I don't know how to fix it. Maybe I have to do a other loop inside this code line, but is it possible ? Thanks for your answers !

Code :

from netCDF4 import Dataset
import matplotlib.pylab as plt
import cartopy.crs as ccrs
import numpy as np
import netCDF4 as nc

month_name=["Jan","Feb","Mar","Apr","May","June","Jul","Aug","Sep","Oct","Nov","Dec"]

data = "/Users/name/Desktop/Internship/tos_1979.nc" 
dset = Dataset(data)
fig = plt.figure(figsize=(24,24))
cmap = plt.cm.jet
for imonth in np.arange(1,13):  
ax = fig.add_subplot(4,3,imonth, projection=ccrs.PlateCarree())   
  tos = dset.variables['tos'][0, :, :]
  lon = dset.variables['longitude'][:]
  lat = dset.variables['latitude'][:]
  time = dset.variables['time'][:]

  ax.set_extent([-70,20,30,90])
  ax.coastlines();
  plt.title('Ocean Surface Temperature :'+ month_name[imonth-1],fontsize=12
  ax.stock_img()
  ax.gridlines()
  ax.pcolormesh(lon, lat, tos, data=dset, cmap=cmap)
plt.savefig('fig_tos_1979.png')
plt.show()

Just to know, I code with Python, on a MacBook (with Spyder).

Also I have problem with the scalebar tool, it's install on my Mac (thanks to the terminal) but when I import this tools it's says :

  ImportError: cannot import name 'scale_bar' from 'scalebar' (/Users/name/opt/anaconda3/lib/python3.9/site-packages/scalebar/__init__.py)

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-02-07 09:16:24

这可能是因为您已经用0对数据的索引进行了硬编码。

所以也许改变了:
tos = dset.Variobles ['tos'] [0,:,:]

to:
tos = dset.Variobles ['tos'] [imonth-1,:,:]

,但我猜是因为我不知道您的数据的布局。

您也可以尝试使用Xarray加载数据,然后利用像这样的图的一些便利功能。请参阅:
https:////docs.xarray.dev/en.dev/en/stable/用户指示/绘图.html#faceTing

仍然在后台使用matplotlib+Cartopy,但在开始时可能会提供更轻松的界面。

(我根本不熟悉比例尺模块...)

It's probably because you have hard coded the indexing of the data with 0.

So perhaps change this:
tos = dset.variables['tos'][0, :, :]

To:
tos = dset.variables['tos'][imonth-1, :, :]

But I'm guessing since I don't know the layout of your data.

You could also try loading the data with Xarray, and then leverage some of the convenience functions for plots like this. See:
https://docs.xarray.dev/en/stable/user-guide/plotting.html#faceting

That still uses Matplotlib+Cartopy in the background, but might provide an easier interface when you're getting started.

(I'm not familiar with the scalebar module at all...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文