I was trying to replicate the Contour Plot:
for a new set of interpolated data found on interpolated.nc I got the error:
JavaScript
x
77
77
1
TypeError Traceback (most recent call last)
2
Input In [552], in <cell line: 48>()
3
43 plt.contourf(to_np(lons), to_np(lats), to_np(dr_nested2), 10,
4
44 transform=crs.PlateCarree(),
5
45 cmap=get_cmap("jet"),extend='both',levels=lvl)
6
47 # Add a color bar
7
---> 48 plt.colorbar(ax=ax, shrink=.98)
8
50 # Add the gridlines
9
51 ax.gridlines(color="black", linestyle="dotted")
10
11
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/pyplot.py:2109, in colorbar(mappable, cax, ax, **kw)
12
2104 if mappable is None:
13
2105 raise RuntimeError('No mappable was found to use for colorbar '
14
2106 'creation. First define a mappable such as '
15
2107 'an image (with imshow) or a contour set ('
16
2108 'with contourf).')
17
-> 2109 ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kw)
18
2110 return ret
19
20
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/figure.py:1210, in FigureBase.colorbar(self, mappable, cax, ax, use_gridspec, **kw)
21
1206 NON_COLORBAR_KEYS = ['fraction', 'pad', 'shrink', 'aspect', 'anchor',
22
1207 'panchor']
23
1208 cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
24
-> 1210 cb = cbar.Colorbar(cax, mappable, **cb_kw)
25
1212 if not userax:
26
1213 self.sca(current_ax)
27
28
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/colorbar.py:483, in Colorbar.__init__(self, ax, mappable, cmap, norm, alpha, values, boundaries, orientation, ticklocation, extend, spacing, ticks, format, drawedges, filled, extendfrac, extendrect, label)
29
480 self.ticklocation = ticklocation
30
482 self.set_label(label)
31
--> 483 self._reset_locator_formatter_scale()
32
485 if np.iterable(ticks):
33
486 self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
34
35
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/colorbar.py:1214, in Colorbar._reset_locator_formatter_scale(self)
36
1212 if self.spacing == 'uniform':
37
1213 funcs = (self._forward_boundaries, self._inverse_boundaries)
38
-> 1214 self._set_scale('function', functions=funcs)
39
1215 elif self.spacing == 'proportional':
40
1216 self._set_scale('linear')
41
42
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/colorbar.py:1027, in Colorbar._set_scale(self, scale, **kwargs)
43
1001 """
44
1002 Set the colorbar long axis scale.
45
1003
46
( )
47
1024 be used here.
48
1025 """
49
1026 if self.orientation == 'vertical':
50
-> 1027 self.ax.set_yscale(scale, **kwargs)
51
1028 else:
52
1029 self.ax.set_xscale(scale, **kwargs)
53
54
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_base.py:4117, in _AxesBase.set_yscale(self, value, **kwargs)
55
4115 g = self.get_shared_y_axes()
56
4116 for ax in g.get_siblings(self):
57
-> 4117 ax.yaxis._set_scale(value, **kwargs)
58
4118 ax._update_transScale()
59
4119 ax.stale = True
60
61
File ~/anaconda3/lib/python3.9/site-packages/matplotlib/axis.py:761, in Axis._set_scale(self, value, **kwargs)
62
759 def _set_scale(self, value, **kwargs):
63
760 if not isinstance(value, mscale.ScaleBase):
64
--> 761 self._scale = mscale.scale_factory(value, self, **kwargs)
65
762 else:
66
763 self._scale = value
67
68
File ~/anaconda3/lib/python3.9/site-packages/proplot/scale.py:903, in _scale_factory(scale, axis, *args, **kwargs)
69
898 if scale not in scales:
70
899 raise ValueError(
71
900 f'Unknown axis scale {scale!r}. Options are '
72
901 + ', '.join(map(repr, scales)) + '.'
73
902 )
74
--> 903 return scales[scale](*args, **kwargs)
75
76
TypeError: __init__() missing 1 required positional argument: 'transform'
77
Nevetheless, I realized that the error was related to the plt.colorbar(ax=ax, shrink=.98)
line, so I was wondering if I could get any help in order to solve this issue, since I need the colourbar in order to do the analysis for a report. I would really appreciate any insights in the problem!
The code I used to make this plot is the following:
JavaScript
1
40
40
1
import numpy as np
2
import matplotlib.pyplot as plt
3
from matplotlib.cm import get_cmap
4
import cartopy.crs as crs
5
import cartopy.feature as cfeature
6
from cartopy.feature import NaturalEarthFeature
7
from netCDF4 import Dataset
8
9
from wrf import (getvar, to_np, vertcross, smooth2d, CoordPair, GeoBounds,
10
get_cartopy, latlon_coords, cartopy_xlim, cartopy_ylim)
11
12
lvl = np.arange(-10, 30, 4)
13
14
lats, lons = latlon_coords(dr_nested2)
15
16
ax = plt.axes(projection=crs.PlateCarree())
17
18
states = NaturalEarthFeature(category="cultural", scale="10m",
19
facecolor="none",
20
name="admin_1_states_provinces")
21
ax.add_feature(states, linewidth=.4, edgecolor="black")
22
23
states = NaturalEarthFeature(category="cultural", scale="10m",
24
facecolor="none",
25
name="admin_1_states_provinces")
26
ax.add_feature(states, linewidth=.4, edgecolor="black")
27
ax.coastlines('10m', linewidth=0.8)
28
29
plt.contour(to_np(lons), to_np(lats), to_np(dr_nested2), 10, colors='#FF000000',
30
transform=crs.PlateCarree())
31
plt.contourf(to_np(lons), to_np(lats), to_np(dr_nested2), 10,
32
transform=crs.PlateCarree(),
33
cmap=get_cmap("jet"),extend='both',levels=lvl)
34
35
plt.colorbar(ax=ax, shrink=.98)
36
37
ax.gridlines(color="black", linestyle="dotted")
38
39
plt.show()
40
The dr_nested2
variable is an xarray.DataArray containing the information of the nc file.
JavaScript
1
20
20
1
array([[19.008703, 18.907665, 18.813095, , 21.384596, 21.471825,
2
21.435888],
3
[19.123476, 18.974825, 18.839777, , 21.634035, 21.84276 ,
4
21.868538],
5
[19.16345 , 19.013046, 18.87523 , , 21.712034, 21.923061,
6
21.974125],
7
,
8
[22.84894 , 22.85399 , 22.89827 , , 22.873253, 22.928793,
9
22.977732],
10
[22.832474, 22.79055 , 22.820337, , 23.111856, 23.178507,
11
23.201542],
12
[22.968447, 23.084492, 23.25534 , , 23.269384, 23.360067,
13
23.377628]], dtype=float32)
14
Coordinates:
15
XTIME datetime64[ns] 2022-03-13
16
* XLAT (XLAT) float64 -25.4 -25.3 -25.2 -25.1 -9.0 -8.9 -8.8 -8.7
17
* XLONG (XLONG) float64 -75.2 -75.1 -75.0 -74.9 -57.8 -57.7 -57.6 -57.5
18
Attributes:
19
regrid_method: patch
20
Advertisement
Answer
Instead of
JavaScript
1
2
1
ax = plt.axes(projection=crs.PlateCarree())
2
It should be this;
JavaScript
1
2
1
ax = plt.axes(transform=crs.PlateCarree())
2