def _label(cube, mode, result=None, ndims=2, coords=None): """Puts labels on the current plot using the given cube.""" plt.title(_title(cube, with_units=False)) if result is not None: draw_edges = mode == iris.coords.POINT_MODE bar = plt.colorbar(result, orientation='horizontal', drawedges=draw_edges) has_known_units = not (cube.units.is_unknown() or cube.units.is_no_unit()) if has_known_units and cube.units != iris.unit.Unit('1'): # Use shortest unit representation for anything other than time if (not cube.units.is_time() and not cube.units.is_time_reference() and len(cube.units.symbol) < len(str(cube.units))): bar.set_label(cube.units.symbol) else: bar.set_label(cube.units) # Remove the tick which is put on the colorbar by default. bar.ax.tick_params(length=0) if coords is None: plot_defn = iplt._get_plot_defn(cube, mode, ndims) else: plot_defn = iplt._get_plot_defn_custom_coords_picked(cube, coords, mode, ndims=ndims) if ndims == 2: if not iplt._can_draw_map(plot_defn.coords): plt.ylabel(_title(plot_defn.coords[0], with_units=True)) plt.xlabel(_title(plot_defn.coords[1], with_units=True)) elif ndims == 1: plt.xlabel(_title(plot_defn.coords[0], with_units=True)) plt.ylabel(_title(cube, with_units=True)) else: raise ValueError('Unexpected number of dimensions (%s) given to _label.' % ndims)
def test_2d_coords(self): cube = simple_2d_w_multidim_coords() defn = iplt._get_plot_defn_custom_coords_picked(cube, ('foo', 'bar'), BOUND_MODE) self.assertEqual([coord.name() for coord in defn.coords], ['bar', 'foo']) self.assertFalse(defn.transpose)
def test_1d_coords_swapped(self): cube = simple_2d() defn = iplt._get_plot_defn_custom_coords_picked(cube, ('bar', 'foo'), POINT_MODE) self.assertEqual([coord.name() for coord in defn.coords], ['foo', 'bar']) self.assertTrue(defn.transpose)
def _label(cube, mode, result=None, ndims=2, coords=None): """Puts labels on the current plot using the given cube.""" plt.title(_title(cube, with_units=False)) if result is not None: draw_edges = mode == iris.coords.POINT_MODE bar = plt.colorbar(result, orientation='horizontal', drawedges=draw_edges) bar.set_label(cube.units) # Remove the tick which is put on the colorbar by default. bar.ax.tick_params(length=0) if coords is None: plot_defn = iplt._get_plot_defn(cube, mode, ndims) else: plot_defn = iplt._get_plot_defn_custom_coords_picked(cube, coords, mode, ndims=ndims) if ndims == 2: if not iplt._can_draw_map(plot_defn.coords): plt.ylabel(_title(plot_defn.coords[0], with_units=True)) plt.xlabel(_title(plot_defn.coords[1], with_units=True)) elif ndims == 1: plt.xlabel(_title(plot_defn.coords[0], with_units=True)) plt.ylabel(_title(cube, with_units=True)) else: raise ValueError('Unexpected number of dimensions (%s) given to _label.' % ndims)
def _label(cube, mode, result=None, ndims=2, coords=None): """Puts labels on the current plot using the given cube.""" plt.title(_title(cube, with_units=False)) if result is not None: draw_edges = mode == iris.coords.POINT_MODE bar = plt.colorbar(result, orientation='horizontal', drawedges=draw_edges) has_known_units = not (cube.units.is_unknown() or cube.units.is_no_unit()) if has_known_units and cube.units != cf_units.Unit('1'): # Use shortest unit representation for anything other than time if _use_symbol(cube.units): bar.set_label(cube.units.symbol) else: bar.set_label(cube.units) # Remove the tick which is put on the colorbar by default. bar.ax.tick_params(length=0) if coords is None: plot_defn = iplt._get_plot_defn(cube, mode, ndims) else: plot_defn = iplt._get_plot_defn_custom_coords_picked( cube, coords, mode, ndims=ndims) if ndims == 2: if not iplt._can_draw_map(plot_defn.coords): plt.ylabel(_title(plot_defn.coords[0], with_units=True)) plt.xlabel(_title(plot_defn.coords[1], with_units=True)) elif ndims == 1: plt.xlabel(_title(plot_defn.coords[0], with_units=True)) plt.ylabel(_title(cube, with_units=True)) else: msg = 'Unexpected number of dimensions (%s) given to _label.' % ndims raise ValueError(msg)
def test_span_check(self): cube = hybrid_height() emsg = 'don\'t span the 2 data dimensions' with self.assertRaisesRegex(ValueError, emsg): iplt._get_plot_defn_custom_coords_picked( cube, ('sigma', 'level_height'), POINT_MODE)
def test_2d_coords_as_integers(self): cube = simple_2d_w_multidim_coords() defn = iplt._get_plot_defn_custom_coords_picked(cube, (0, 1), BOUND_MODE) self.assertEqual([coord for coord in defn.coords], [1, 0]) self.assertTrue(defn.transpose)
def test_1d_coords_as_integers_swapped(self): cube = simple_2d() defn = iplt._get_plot_defn_custom_coords_picked(cube, (0, 1), POINT_MODE) self.assertEqual([coord for coord in defn.coords], [1, 0]) self.assertTrue(defn.transpose)
def test_1d_coords_as_integers(self): cube = simple_2d() defn = iplt._get_plot_defn_custom_coords_picked(cube, (1, 0), POINT_MODE) self.assertEqual([coord for coord in defn.coords], [0, 1]) self.assertFalse(defn.transpose)
def test_span_check(self): cube = hybrid_height() emsg = 'don\'t span the 2 data dimensions' with self.assertRaisesRegexp(ValueError, emsg): iplt._get_plot_defn_custom_coords_picked( cube, ('sigma', 'level_height'), POINT_MODE)
def test_2d_coords_as_integers(self): cube = simple_2d_w_multidim_coords() defn = iplt._get_plot_defn_custom_coords_picked(cube, (0, 1), BOUND_MODE) self.assertEqual([coord for coord in defn.coords], [1, 0]) self.assertTrue(defn.transpose)
def test_1d_coords_as_integers_swapped(self): cube = simple_2d() defn = iplt._get_plot_defn_custom_coords_picked(cube, (0, 1), POINT_MODE) self.assertEqual([coord for coord in defn.coords], [1, 0]) self.assertTrue(defn.transpose)
def test_1d_coords_as_integers(self): cube = simple_2d() defn = iplt._get_plot_defn_custom_coords_picked(cube, (1, 0), POINT_MODE) self.assertEqual([coord for coord in defn.coords], [0, 1]) self.assertFalse(defn.transpose)