def test_to_dataset(self): unnamed = DataArray([1, 2], dims='x') actual = unnamed.to_dataset() expected = Dataset({None: ('x', [1, 2])}) self.assertDatasetIdentical(expected, actual) self.assertIsNot(unnamed._dataset, actual) actual = unnamed.to_dataset('foo') expected = Dataset({'foo': ('x', [1, 2])}) self.assertDatasetIdentical(expected, actual) named = DataArray([1, 2], dims='x', name='foo') actual = named.to_dataset() expected = Dataset({'foo': ('x', [1, 2])}) self.assertDatasetIdentical(expected, actual) actual = named.to_dataset('bar') expected = Dataset({'bar': ('x', [1, 2])}) self.assertDatasetIdentical(expected, actual)
def setUp(self): da = DataArray(easy_array((10, 15), start=-1), dims=['y', 'x']) # add 2d coords ds = da.to_dataset(name='testvar') x, y = np.meshgrid(da.x.values, da.y.values) ds['x2d'] = DataArray(x, dims=['y', 'x']) ds['y2d'] = DataArray(y, dims=['y', 'x']) ds.set_coords(['x2d', 'y2d'], inplace=True) # set darray and plot method self.darray = ds.testvar self.plotmethod = getattr(self.darray.plot, self.plotfunc.__name__)
def setUp(self): da = DataArray(easy_array( (10, 15), start=-1), dims=['y', 'x']) # add 2d coords ds = da.to_dataset(name='testvar') x, y = np.meshgrid(da.x.values, da.y.values) ds['x2d'] = DataArray(x, dims=['y', 'x']) ds['y2d'] = DataArray(y, dims=['y', 'x']) ds.set_coords(['x2d', 'y2d'], inplace=True) # set darray and plot method self.darray = ds.testvar self.plotmethod = getattr(self.darray.plot, self.plotfunc.__name__)
class TestFacetGrid(PlotTestCase): def setUp(self): d = easy_array((10, 15, 3)) self.darray = DataArray(d, dims=['y', 'x', 'z'], coords={'z': ['a', 'b', 'c']}) self.g = xplt.FacetGrid(self.darray, col='z') def test_no_args(self): self.g.map_dataarray(xplt.contourf, 'x', 'y') # Don't want colorbar labeled with 'None' alltxt = text_in_fig() self.assertNotIn('None', alltxt) for ax in self.g.axes.flat: self.assertTrue(ax.has_data()) # default font size should be small fontsize = ax.title.get_size() self.assertLessEqual(fontsize, 12) def test_names_appear_somewhere(self): self.darray.name = 'testvar' self.g.map_dataarray(xplt.contourf, 'x', 'y') for k, ax in zip('abc', self.g.axes.flat): self.assertEqual('z = {0}'.format(k), ax.get_title()) alltxt = text_in_fig() self.assertIn(self.darray.name, alltxt) for label in ['x', 'y']: self.assertIn(label, alltxt) def test_text_not_super_long(self): self.darray.coords['z'] = [100 * letter for letter in 'abc'] g = xplt.FacetGrid(self.darray, col='z') g.map_dataarray(xplt.contour, 'x', 'y') alltxt = text_in_fig() maxlen = max(len(txt) for txt in alltxt) self.assertLess(maxlen, 50) t0 = g.axes[0, 0].get_title() self.assertTrue(t0.endswith('...')) def test_colorbar(self): vmin = self.darray.values.min() vmax = self.darray.values.max() expected = np.array((vmin, vmax)) self.g.map_dataarray(xplt.imshow, 'x', 'y') for image in plt.gcf().findobj(mpl.image.AxesImage): clim = np.array(image.get_clim()) self.assertTrue(np.allclose(expected, clim)) self.assertEqual(1, len(find_possible_colorbars())) def test_empty_cell(self): g = xplt.FacetGrid(self.darray, col='z', col_wrap=2) g.map_dataarray(xplt.imshow, 'x', 'y') bottomright = g.axes[-1, -1] self.assertFalse(bottomright.has_data()) self.assertFalse(bottomright.get_visible()) def test_norow_nocol_error(self): with self.assertRaisesRegexp(ValueError, r'[Rr]ow'): xplt.FacetGrid(self.darray) def test_groups(self): self.g.map_dataarray(xplt.imshow, 'x', 'y') upperleft_dict = self.g.name_dicts[0, 0] upperleft_array = self.darray.loc[upperleft_dict] z0 = self.darray.isel(z=0) self.assertDataArrayEqual(upperleft_array, z0) def test_float_index(self): self.darray.coords['z'] = [0.1, 0.2, 0.4] g = xplt.FacetGrid(self.darray, col='z') g.map_dataarray(xplt.imshow, 'x', 'y') def test_nonunique_index_error(self): self.darray.coords['z'] = [0.1, 0.2, 0.2] with self.assertRaisesRegexp(ValueError, r'[Uu]nique'): xplt.FacetGrid(self.darray, col='z') def test_robust(self): z = np.zeros((20, 20, 2)) darray = DataArray(z, dims=['y', 'x', 'z']) darray[:, :, 1] = 1 darray[2, 0, 0] = -1000 darray[3, 0, 0] = 1000 g = xplt.FacetGrid(darray, col='z') g.map_dataarray(xplt.imshow, 'x', 'y', robust=True) # Color limits should be 0, 1 # The largest number displayed in the figure should be less than 21 numbers = set() alltxt = text_in_fig() for txt in alltxt: try: numbers.add(float(txt)) except ValueError: pass largest = max(abs(x) for x in numbers) self.assertLess(largest, 21) def test_can_set_vmin_vmax(self): vmin, vmax = 50.0, 1000.0 expected = np.array((vmin, vmax)) self.g.map_dataarray(xplt.imshow, 'x', 'y', vmin=vmin, vmax=vmax) for image in plt.gcf().findobj(mpl.image.AxesImage): clim = np.array(image.get_clim()) self.assertTrue(np.allclose(expected, clim)) def test_figure_size(self): self.assertArrayEqual(self.g.fig.get_size_inches(), (10, 3)) g = xplt.FacetGrid(self.darray, col='z', size=6) self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) g = self.darray.plot.imshow(col='z', size=6) self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) g = xplt.FacetGrid(self.darray, col='z', size=4, aspect=0.5) self.assertArrayEqual(g.fig.get_size_inches(), (7, 4)) def test_num_ticks(self): nticks = 100 maxticks = nticks + 1 self.g.map_dataarray(xplt.imshow, 'x', 'y') self.g.set_ticks(max_xticks=nticks, max_yticks=nticks) for ax in self.g.axes.flat: xticks = len(ax.get_xticks()) yticks = len(ax.get_yticks()) self.assertLessEqual(xticks, maxticks) self.assertLessEqual(yticks, maxticks) self.assertGreaterEqual(xticks, nticks / 2.0) self.assertGreaterEqual(yticks, nticks / 2.0) def test_map(self): self.g.map(plt.contourf, 'x', 'y', Ellipsis) self.g.map(lambda: None) def test_map_dataset(self): g = xplt.FacetGrid(self.darray.to_dataset(name='foo'), col='z') g.map(plt.contourf, 'x', 'y', 'foo') alltxt = text_in_fig() for label in ['x', 'y']: self.assertIn(label, alltxt) # everything has a label self.assertNotIn('None', alltxt) # colorbar can't be inferred automatically self.assertNotIn('foo', alltxt) self.assertEqual(0, len(find_possible_colorbars())) g.add_colorbar(label='colors!') self.assertIn('colors!', text_in_fig()) self.assertEqual(1, len(find_possible_colorbars())) def test_set_axis_labels(self): g = self.g.map_dataarray(xplt.contourf, 'x', 'y') g.set_axis_labels('longitude', 'latitude') alltxt = text_in_fig() for label in ['longitude', 'latitude']: self.assertIn(label, alltxt) def test_facetgrid_colorbar(self): a = easy_array((10, 15, 4)) d = DataArray(a, dims=['y', 'x', 'z'], name='foo') d.plot.imshow(x='x', y='y', col='z') self.assertEqual(1, len(find_possible_colorbars())) d.plot.imshow(x='x', y='y', col='z', add_colorbar=True) self.assertEqual(1, len(find_possible_colorbars())) d.plot.imshow(x='x', y='y', col='z', add_colorbar=False) self.assertEqual(0, len(find_possible_colorbars()))