matplotlib / basemap

Plot on map projections (with coastlines and political boundaries) using matplotlib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Another problem in MacOS

CaffreyR opened this issue · comments

@molinav Hi! Something wrong with colorbar! See I draw the nc file on the plot, but when I try to join together , it go white completely!
截屏2022-04-27 下午4 40 49
截屏2022-04-27 下午4 41 00
截屏2022-04-27 下午4 41 06

@CaffreyR Can you provide the complete code snippet that you were trying to run? You can copy the snippet here enclosed by triplets of backticks (```). Is the SST dataset available somewhere or can you provide it? I tried to run your IPython lines in my console with a dummy image sst = np.random.rand(100, 200) and I could not reproduce the problem.

Absolutely, the code is here. Actually, the nc file is from NASA, which describe sea temperature. @molinav

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fh = Dataset('sst.day.mean.2021.nc', mode='r')
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
sst = fh.variables['sst'][:]

sst_units = fh.variables['sst'].units
lon_0 = lons.mean()
lat_0 = lats.mean()
fig=plt.figure(figsize=(18,10))
m = Basemap(lat_0=lat_0, lon_0=lon_0)
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)
<Figure size 1296x720 with 0 Axes>
sst_0 = sst[0:1:, ::, ::]
cs = m.pcolor(xi, yi, np.squeeze(sst_0))
plt.savefig("0.png")
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>


output_4_1

m.drawparallels(np.arange(-90., 91., 20.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 40.), labels=[0,0,0,1], fontsize=10)
{20.0: ([<matplotlib.lines.Line2D at 0x2b24519c0>],
  [Text(20.16, -93.6, '20°E')]),
 60.0: ([<matplotlib.lines.Line2D at 0x2b2451c30>],
  [Text(60.12, -93.6, '60°E')]),
 100.0: ([<matplotlib.lines.Line2D at 0x2b2451f30>],
  [Text(100.08, -93.6, '100°E')]),
 140.0: ([<matplotlib.lines.Line2D at 0x2b2452200>],
  [Text(140.04, -93.6, '140°E')]),
 180.0: ([<matplotlib.lines.Line2D at 0x2b24524d0>],
  [Text(180.35999999999999, -93.6, '180°')]),
 220.0: ([<matplotlib.lines.Line2D at 0x2b24527a0>],
  [Text(220.32, -93.6, '140°W')]),
 260.0: ([<matplotlib.lines.Line2D at 0x2b2452a70>],
  [Text(260.28, -93.6, '100°W')]),
 300.0: ([<matplotlib.lines.Line2D at 0x2b2452d40>],
  [Text(300.24, -93.6, '60°W')]),
 340.0: ([<matplotlib.lines.Line2D at 0x2b2453010>],
  [Text(340.2, -93.6, '20°W')])}


output_5_1

m.drawcoastlines()
m.drawstates()
m.drawcountries()
<matplotlib.collections.LineCollection at 0x2b24dd690>


output_6_1

cbar = m.colorbar(cs)
cbar.set_label(sst_units)


output_7_0

plt.title('Surface Air Temperature')
plt.show()


output_8_0

@CaffreyR Unfortunately I cannot reproduce the problem. I am running this (with the same file from NASA as yours):

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset

fh = Dataset("sst.day.mean.2021.nc", mode="r")
lons = fh.variables["lon"][:]
lats = fh.variables["lat"][:]
sst = fh.variables["sst"][:]

sst_units = fh.variables["sst"].units
lon_0 = lons.mean()
lat_0 = lats.mean()

fig = plt.figure(figsize=(18,10))
m = Basemap(lat_0=lat_0, lon_0=lon_0)
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)

sst_0 = sst[0:1:, ::, ::]
cs = m.pcolor(xi, yi, np.squeeze(sst_0))

m.drawparallels(np.arange(-90., 91., 20.), labels=[1, 0, 0, 0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 40.), labels=[0, 0, 0, 1], fontsize=10)

m.drawcoastlines()
m.drawstates()
m.drawcountries()

cbar = m.colorbar(cs)
cbar.set_label(sst_units)

plt.title("Surface Air Temperature")
plt.show()

and I am getting what you were expecting:
sst2021

I am using the following libraries:

numpy:      1.22.3
matplotlib: 3.4.3
basemap:    1.3.2+dev
netCDF4:    1.5.5.1

Wait a minute, when I join all the code block together, it shows the picture I expected! I guess maybe something wrong with the console or jupyter notebook? @molinav

It looks like each code block is using its own figure and canvas. If you look at your previous message, every figure is losing whatever you drew before. In the first figure, you plot the SST data. In the second one, you get the parallels and meridians but lose the SST data. In the third one, you get the coastlines and boundaries but lose the parallels and meridians. And so on.

Yep! It seems the problems solved! Thanks!