Skip to content

Commit 6c9e4d4

Browse files
matplotlib 3.8: fix get_cmap deprecation
1 parent 1af2639 commit 6c9e4d4

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

climada/engine/forecast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from matplotlib.ticker import PercentFormatter, ScalarFormatter
3232
from matplotlib.colors import ListedColormap, BoundaryNorm
3333
import cartopy.crs as ccrs
34+
from matplotlib import colormaps as cm
3435
import pyproj
3536
import shapely
3637
from cartopy.io import shapereader
@@ -88,7 +89,7 @@
8889
warnprob_colors_extended = np.repeat(warnprob_colors, 10, axis=0)
8990
CMAP_WARNPROB = ListedColormap(warnprob_colors_extended)
9091
# colors for impact forecast
91-
color_map_pre = plt.get_cmap("plasma", 90)
92+
color_map_pre = cm.get_cmap("plasma").resampled(90)
9293
impact_colors = color_map_pre(np.linspace(0, 1, 90))
9394
white_extended = np.repeat([[255 / 255, 255 / 255, 255 / 255, 1]], 10, axis=0)
9495
impact_colors_extended = np.append(white_extended, impact_colors, axis=0)

climada/engine/unsequa/unc_output.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import numpy as np
3434
import matplotlib.pyplot as plt
3535
import matplotlib as mpl
36+
from matplotlib import colormaps as cm
3637

3738
from climada import CONFIG
3839

@@ -1034,7 +1035,7 @@ def plot_sensitivity_map(self, salib_si='S1', **kwargs):
10341035
if len(n) > 0 :
10351036
n = n[0]
10361037
cmap = mpl.colors.ListedColormap(
1037-
plt.get_cmap(MAP_CMAP).colors[:len(labels)]
1038+
cm.get_cmap(MAP_CMAP).colors[:len(labels)]
10381039
)
10391040
colors = list(cmap.colors)
10401041
colors[n] = tuple(np.repeat(0.93, 3))

climada/entity/measures/measure_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import logging
2727
from typing import Optional, List
2828

29-
import matplotlib.pyplot as plt
29+
from matplotlib import colormaps as cm
3030
import numpy as np
3131
import pandas as pd
3232
import xlsxwriter
@@ -310,7 +310,7 @@ def check(self):
310310
ValueError
311311
"""
312312
for key_haz, meas_dict in self._data.items():
313-
def_color = plt.cm.get_cmap('Greys', len(meas_dict))
313+
def_color = cm.get_cmap('Greys').resampled(len(meas_dict))
314314
for i_meas, (name, meas) in enumerate(meas_dict.items()):
315315
if (name != meas.name) | (name == ''):
316316
raise ValueError("Wrong Measure.name: %s != %s."

climada/util/plot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import numpy as np
3737
import matplotlib.pyplot as plt
3838
import matplotlib as mpl
39+
from matplotlib import colormaps as cm
3940
from mpl_toolkits.axes_grid1 import make_axes_locatable
4041
from shapely.geometry import box
4142
import cartopy.crs as ccrs
@@ -440,10 +441,10 @@ def geo_scatter_categorical(array_sub, geo_coord, var_name, title,
440441
if cmap_name in ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
441442
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']:
442443
cmap = mpl.colors.ListedColormap(
443-
mpl.cm.get_cmap(cmap_name).colors[:array_sub_n]
444+
cm.get_cmap(cmap_name).colors[:array_sub_n]
444445
)
445446
else:
446-
cmap = mpl.cm.get_cmap(cmap_arg, array_sub_n)
447+
cmap = cm.get_cmap(cmap_arg).resampled(array_sub_n)
447448
elif isinstance(cmap_arg, mpl.colors.ListedColormap):
448449
# If a user brings their own colormap it's probably qualitative
449450
cmap_name = 'defined by the user'
@@ -456,7 +457,7 @@ def geo_scatter_categorical(array_sub, geo_coord, var_name, title,
456457
# default qualitative colormap
457458
cmap_name = CMAP_CAT
458459
cmap = mpl.colors.ListedColormap(
459-
mpl.cm.get_cmap(cmap_name).colors[:array_sub_n]
460+
cm.get_cmap(cmap_name).colors[:array_sub_n]
460461
)
461462

462463
if array_sub_n > cmap.N:

climada/util/test/test_plot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import cartopy
2424
import numpy as np
2525
import matplotlib.pyplot as plt
26+
from matplotlib import colormaps as cm
2627
import cartopy.crs as ccrs
2728

2829
import climada.util.plot as u_plot
@@ -67,7 +68,7 @@ def test_geo_scatter_categorical(self):
6768
1: 'int',
6869
2.0: 'float',
6970
'a': 'string'},
70-
pop_name=False, cmap=plt.get_cmap('Set1'))
71+
pop_name=False, cmap=cm.get_cmap('Set1'))
7172
plt.close()
7273

7374
#test colormap warning

script/applications/eca_san_salvador/functions_ss.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import contextily as ctx
2323
import geopandas as gpd
2424
import matplotlib.patches as patches
25+
from matplotlib import colormaps as cm
2526
from shapely import wkt
2627

2728
def plot_salvador_ma():
@@ -110,11 +111,11 @@ def plot_exposure_ss(exposures, point=None):
110111
ax.set_ylim(1536680.51725147, 1539512.429812354)
111112
else:
112113
# create new map for viviendas
113-
cmap_viv = plt.cm.get_cmap('autumn', 4)
114+
cmap_viv = cm.get_cmap('autumn').resampled(4)
114115
cmap_viv = mpl.colors.LinearSegmentedColormap.from_list('Custom cmap',
115116
[cmap_viv(i) for i in range(cmap_viv.N)], cmap_viv.N)
116117
# create new map for aups
117-
cmap_aup = plt.cm.get_cmap('winter', 4)
118+
cmap_aup = cm.get_cmap('winter').resampled(4)
118119
cmap_aup = mpl.colors.LinearSegmentedColormap.from_list('Custom cmap',
119120
[cmap_aup(i) for i in range(cmap_aup.N)], cmap_aup.N)
120121

0 commit comments

Comments
 (0)