Skip to content

Commit 8a31c01

Browse files
committed
allow extend in colorbar
1 parent 9ade596 commit 8a31c01

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

climada/entity/exposures/base.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,28 @@ def check(self):
128128
self._check_optionals(num_exp)
129129
self._check_defaults(num_exp)
130130

131-
def plot(self, ignore_null=True, pop_name=True, buffer_deg=None,
131+
def plot(self, ignore_zero=True, pop_name=True, buffer_deg=1.0,
132132
**kwargs):
133133
"""Plot exposures values sum binned over Earth's map.
134134
135135
Parameters:
136-
ignore_null (bool, optional): flag to indicate if zero and negative
136+
ignore_zero (bool, optional): flag to indicate if zero and negative
137137
values are ignored in plot. Default: False
138138
pop_name (bool, optional): add names of the populated places
139139
buffer_deg (float, optional): border to add to coordinates.
140-
Default: BUFFER_DEG=1 in plot module.
140+
Default: 1.0.
141141
kwargs (optional): arguments for hexbin matplotlib function
142142
143143
Returns:
144144
matplotlib.figure.Figure, cartopy.mpl.geoaxes.GeoAxesSubplot
145145
"""
146-
if ignore_null:
147-
pos_vals = self.value > 0
148-
else:
149-
pos_vals = np.ones((self.value.size,), dtype=bool)
150146
title = self.tag.join_descriptions()
151147
cbar_label = 'Value (%s)' % self.value_unit
152148
if 'reduce_C_function' not in kwargs:
153149
kwargs['reduce_C_function'] = np.sum
154-
if buffer_deg is not None:
155-
return plot.geo_bin_from_array(self.value[pos_vals], \
156-
self.coord[pos_vals], cbar_label, title, pop_name, buffer_deg,\
157-
**kwargs)
158-
return plot.geo_bin_from_array(self.value[pos_vals], \
159-
self.coord[pos_vals], cbar_label, title, pop_name, **kwargs)
150+
151+
return plot.geo_bin_from_array(self.value, self.coord, cbar_label,
152+
title, pop_name, buffer_deg, ignore_zero, **kwargs)
160153

161154
def read(self, files, descriptions='', var_names=None):
162155
"""Read and check exposures.

climada/util/plot.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
MAX_BINS = 200
2828

2929
def geo_bin_from_array(array_sub, geo_coord, var_name, title, pop_name=True,
30-
buffer_deg=BUFFER_DEG, **kwargs):
30+
buffer_deg=BUFFER_DEG, ignore_zero=False, **kwargs):
3131
"""Plot array values binned over input coordinates.
3232
3333
Parameters:
@@ -45,6 +45,7 @@ def geo_bin_from_array(array_sub, geo_coord, var_name, title, pop_name=True,
4545
array_sub.
4646
pop_name (bool, optional): add names of the populated places.
4747
buffer_deg (float, optional): border to add to coordinates
48+
ignore_zero (bool, optional): ignore zero and negative values
4849
kwargs (optional): arguments for hexbin matplotlib function
4950
5051
Returns:
@@ -69,6 +70,14 @@ def geo_bin_from_array(array_sub, geo_coord, var_name, title, pop_name=True,
6970
if coord.shape[0] != array_im.size:
7071
raise ValueError("Size mismatch in input array: %s != %s." % \
7172
(coord.shape[0], array_im.size))
73+
if ignore_zero:
74+
pos_vals = array_im > 0
75+
array_im = array_im[pos_vals]
76+
coord = coord[pos_vals, :]
77+
kwargs_cbar = {'extend':'min'}
78+
else:
79+
kwargs_cbar = {}
80+
7281
# Binned image with coastlines
7382
extent = get_borders(coord)
7483
extent = ([extent[0] - buffer_deg, extent[1] + buffer_deg, extent[2] -\
@@ -86,7 +95,8 @@ def geo_bin_from_array(array_sub, geo_coord, var_name, title, pop_name=True,
8695
# Create colorbar in this axis
8796
cbax = make_axes_locatable(axis).append_axes('right', size="6.5%", \
8897
pad=0.1, axes_class=plt.Axes)
89-
cbar = plt.colorbar(hex_bin, cax=cbax, orientation='vertical')
98+
cbar = plt.colorbar(hex_bin, cax=cbax, orientation='vertical',
99+
**kwargs_cbar)
90100
cbar.set_label(name)
91101
axis.set_title(tit)
92102

0 commit comments

Comments
 (0)