颜色条中奇怪的范围值,matplotlib

发布于 2024-09-28 07:37:23 字数 1241 浏览 1 评论 0原文

我是 python & 的新用户matplotlib,这可能是一个简单的问题,但我在互联网上搜索了几个小时,但找不到解决方案。

我正在绘制 NetCDF 格式的降水数据。我觉得奇怪的是,数据中没有任何负值。(我检查了很多次,只是为了确保)。但颜色栏中的值以负值开头(例如-0.0000312等)。这是没有意义的,因为我不对数据进行任何操作,只是从大文件中选择一部分数据并绘制它。

所以我的代码没什么用。这是代码:

from mpl_toolkits.basemap import Basemap  
import numpy as np  
import matplotlib.pyplot as plt  
from netCDF4 import Dataset  
cd progs  
f=Dataset('V21_GPCP.1979-2009.nc')  
lats=f.variables['lat'][:]  
lons=f.variables['lon'][:]  
prec=f.variables['PREC'][:]  
la=lats[31:52]  
lo=lons[18:83]  
pre=prec[0,31:52,18:83]  
m = Basemap(width=06.e6,height=05.e6,projection='gnom',lat_0=15.,lon_0=80.)  
x, y = m(*np.meshgrid(lo,la))  
m.drawcoastlines()  
m.drawmapboundary(fill_color='lightblue')  
m.drawparallels(np.arange(-90.,120.,5.),labels=[1,0,0,0])  
m.drawmeridians(np.arange(0.,420.,5.),labels=[0,0,0,1])  
cs=m.contourf(x,y,pre,50,cmap=plt.cm.jet)  
plt.colorbar()  

我得到的该代码的输出是一个漂亮的图,颜色条从值 -0.00001893 开始,其余的都是正值,我相信是正确的。这只是困扰我的最小值。

我想知道:

  1. 我的代码有什么问题吗?因为我知道数据是正确的。
  2. 有没有办法手动将值更改为0?
  3. 每次运行代码时,颜色栏中的值都会改变吗?因为对于相同的数据,下次运行代码时,值会像这样“-0.00001893、2.00000000、4.00000000、6.00000000等”
  4. 我想将它们自定义为“0.0、2.0、4.0、6.0 等”

谢谢, 外术

I am a new user to the python & matplotlib, this might be a simple question but I searched the internet for hours and couldn't find a solution for this.

I am plotting precipitation data from which is in the NetCDF format. What I find weird is that, the data doesn't have any negative values in it.(I checked that many times,just to make sure). But the value in the colorbar starts with a negative value (like -0.0000312 etc). It doesnt make sense because I dont do any manipulations to the data, other that just selecting a part of the data from the big file and plotting it.

So my code doesn't much to it. Here is the code:

from mpl_toolkits.basemap import Basemap  
import numpy as np  
import matplotlib.pyplot as plt  
from netCDF4 import Dataset  
cd progs  
f=Dataset('V21_GPCP.1979-2009.nc')  
lats=f.variables['lat'][:]  
lons=f.variables['lon'][:]  
prec=f.variables['PREC'][:]  
la=lats[31:52]  
lo=lons[18:83]  
pre=prec[0,31:52,18:83]  
m = Basemap(width=06.e6,height=05.e6,projection='gnom',lat_0=15.,lon_0=80.)  
x, y = m(*np.meshgrid(lo,la))  
m.drawcoastlines()  
m.drawmapboundary(fill_color='lightblue')  
m.drawparallels(np.arange(-90.,120.,5.),labels=[1,0,0,0])  
m.drawmeridians(np.arange(0.,420.,5.),labels=[0,0,0,1])  
cs=m.contourf(x,y,pre,50,cmap=plt.cm.jet)  
plt.colorbar()  

The output that I got for that code was a beautiful plot, with the colorbar starting from the value -0.00001893, and the rest are positive values, and I believe are correct. Its just the minimum value thats bugging me.

I would like to know:

  1. Is there anything wrong in my code? cos I know that the data is right.
  2. Is there a way to manually change the value to 0?
  3. Is it right for the values in the colorbar to change everytime we run the code, cos for the same data, the next time I run the code, the values go like this " -0.00001893, 2.00000000, 4.00000000, 6.00000000 etc"
  4. I want to customize them to "0.0, 2.0, 4.0, 6.0 etc"

Thanks,
Vaishu

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

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

发布评论

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

评论(1

千鲤 2024-10-05 07:37:23

是的,您可以手动设置有关颜色栏的所有内容的格式。请参阅:

import matplotlib.colors as mc
import matplotlib.pyplot as plt
plt.imshow(X, norm=mc.Normalize(vmin=0))
plt.colorbar(ticks=[0,2,4,6], format='%0.2f')
  1. 许多绘图函数(包括 imshowcontourf 等)都包含采用 NormalizeNormalize 参数代码>对象。您可以设置该对象的 vminvmax 属性来调整颜色条的相应值。

  2. colorbar 采用 ticksformat 参数来调整要显示的刻度以及显示方式。

Yes, you can manually format everything about the colorbar. See this:

import matplotlib.colors as mc
import matplotlib.pyplot as plt
plt.imshow(X, norm=mc.Normalize(vmin=0))
plt.colorbar(ticks=[0,2,4,6], format='%0.2f')
  1. Many plotting functions including imshow, contourf, and others include a norm argument that takes a Normalize object. You can set the vmin or vmax attributes of that object to adjust the corresponding values of the colorbar.

  2. colorbar takes the ticks and format arguments to adjust which ticks to display and how to display them.

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