爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 5572|回复: 12

[求助] 绘制风场图有问题Python

[复制链接]

新浪微博达人勋

发表于 2022-11-28 23:21:46 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
初学Python,想绘制500hPa风场图,怎么都画不出来,各位大神帮忙看一下~
import xarray as xr
import numpy as np
import datetime as dt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.mpl.ticker as cticker
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from datetime import datetime

ds=xr.open_dataset(r'F:\lunwen\2022-4\zl\20200525.nc')
time=ds.variables['time'][:]
u=ds.u.loc['2020-05-23':'2020-05-27',500]
v=ds.v.loc['2020-05-23':'2020-05-27',500]

lat=ds.variables['latitude'][:]
lon=ds.variables['longitude'][:]
lons,lats=np.meshgrid(lon,lat)

fig = plt.figure(figsize=(12,8))
proj = ccrs.PlateCarree(central_longitude=95)
leftlon, rightlon, lowerlat, upperlat = (50,140,0,60)
img_extent = [leftlon, rightlon, lowerlat, upperlat]

ax = fig.add_axes([0.1, 0.1, 0.8, 0.6],projection = proj)
ax.set_extent(img_extent, crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE)
ax.set_xticks(np.arange(leftlon,rightlon+20,20), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(lowerlat,upperlat+20,20), crs=ccrs.PlateCarree())
lon_formatter = cticker.LongitudeFormatter()
lat_formatter = cticker.LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
ax.barbs(lons[::2],lats[::2],u[0,::2,::2],v[0,::2,::2],pivot='middle',colors='k',transform=ccrs.PlateCarree())

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2022-11-29 07:50:09 | 显示全部楼层
没报错信息吗?只出了一张空白的图吗?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-11-29 15:30:34 | 显示全部楼层
linzy 发表于 2022-11-29 07:50
没报错信息吗?只出了一张空白的图吗?

只能绘制出地理地图,报错的信息如下,帮忙看一下~
ValueError: x, y, u and v arrays must be the same shape
D:\Python\Anaconda\lib\site-packages\cartopy\crs.py:245: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
  if len(multi_line_string) > 1:
D:\Python\Anaconda\lib\site-packages\cartopy\crs.py:297: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.
  for line in multi_line_string:
D:\Python\Anaconda\lib\site-packages\cartopy\crs.py:364: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
  if len(p_mline) > 0:
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-11-30 12:22:41 | 显示全部楼层
lons[::2]和lat[::2]改为lons[::2,::2]和lats[::2,::2]试一下?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-11-30 14:54:58 | 显示全部楼层
怪力金鱼姬 发表于 2022-11-29 15:30
只能绘制出地理地图,报错的信息如下,帮忙看一下~
ValueError: x, y, u and v arrays must be the same ...

你用楼下那个应该就能解决了
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-11-30 20:15:20 | 显示全部楼层
lbj2361 发表于 2022-11-30 12:22
lons[::2]和lat[::2]改为lons[::2,::2]和lats[::2,::2]试一下?

您好,还是出不来风场,帮忙再看一下
报错信息:Argument 'x' has incorrect type (expected numpy.ndarray, got DataArray)
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-11-30 20:21:43 | 显示全部楼层
linzy 发表于 2022-11-30 14:54
你用楼下那个应该就能解决了

您好,还是出不来风场,帮忙再看一下
报错信息:Argument 'x' has incorrect type (expected numpy.ndarray, got DataArray)
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-11-30 20:52:13 | 显示全部楼层
怪力金鱼姬 发表于 2022-11-30 20:21
您好,还是出不来风场,帮忙再看一下
报错信息:Argument 'x' has incorrect type (expected numpy.ndar ...

这个问题后面加变量后面 .data 就行了;原因就是这些变量的类型是Dataarray ,需要转类型。你可以 type(lons)看他的类型,以此类推。他需要类型为ndarray才能运行
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-11-30 20:56:35 | 显示全部楼层
linzy 发表于 2022-11-30 20:52
这个问题后面加变量后面 .data 就行了;原因就是这些变量的类型是Dataarray ,需要转类型。你可以 type(l ...

这个 .data 是加在类型是DataArray的后面,就可以输出类型为ndarray的数组出来
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-11-30 22:06:32 | 显示全部楼层
linzy 发表于 2022-11-30 20:52
这个问题后面加变量后面 .data 就行了;原因就是这些变量的类型是Dataarray ,需要转类型。你可以 type(l ...

对,或者是.values也可以
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表