本文整理汇总了Python中xarray.DataArray.plot方法的典型用法代码示例。如果您正苦于以下问题:Python DataArray.plot方法的具体用法?Python DataArray.plot怎么用?Python DataArray.plot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xarray.DataArray
的用法示例。
在下文中一共展示了DataArray.plot方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_datetime_dimension
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_datetime_dimension(self):
nrow = 3
ncol = 4
time = pd.date_range("2000-01-01", periods=nrow)
a = DataArray(easy_array((nrow, ncol)), coords=[("time", time), ("y", range(ncol))])
a.plot()
ax = plt.gca()
self.assertTrue(ax.has_data())
示例2: test_convenient_facetgrid_4d
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_convenient_facetgrid_4d(self):
a = easy_array((10, 15, 2, 3))
d = DataArray(a, dims=["y", "x", "columns", "rows"])
g = d.plot(x="x", y="y", col="columns", row="rows")
self.assertArrayEqual(g.axes.shape, [3, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d.plot(x="x", y="y", col="columns", ax=plt.gca())
示例3: test_convenient_facetgrid
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_convenient_facetgrid(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=["y", "x", "z"])
d.coords["z"] = list("abcd")
g = d.plot(x="x", y="y", col="z", col_wrap=2, cmap="cool")
self.assertArrayEqual(g.axes.shape, [2, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d.plot(x="x", y="y", col="z", ax=plt.gca())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d[0].plot(x="x", y="y", col="z", ax=plt.gca())
示例4: test_subplot_kws
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=["y", "x", "z"])
d.coords["z"] = list("abcd")
g = d.plot(x="x", y="y", col="z", col_wrap=2, cmap="cool", subplot_kws=dict(axisbg="r"))
for ax in g.axes.flat:
self.assertEqual(ax.get_axis_bgcolor(), "r")
示例5: test_subplot_kws
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool',
subplot_kws=dict(axisbg='r'))
for ax in g.axes.flat:
self.assertEqual(ax.get_axis_bgcolor(), 'r')
示例6: test_subplot_kws
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool',
subplot_kws=dict(axisbg='r'))
for ax in g.axes.flat:
try:
# mpl V2
self.assertEqual(ax.get_facecolor()[0:3],
mpl.colors.to_rgb('r'))
except AttributeError:
self.assertEqual(ax.get_axis_bgcolor(), 'r')
示例7: TestPlot1D
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestPlot1D(PlotTestCase):
def setUp(self):
d = [0, 1.1, 0, 2]
self.darray = DataArray(d, coords={'period': range(len(d))},
dims='period')
def test_xlabel_is_index_name(self):
self.darray.plot()
self.assertEqual('period', plt.gca().get_xlabel())
def test_no_label_name_on_y_axis(self):
self.darray.plot()
self.assertEqual('', plt.gca().get_ylabel())
def test_ylabel_is_data_name(self):
self.darray.name = 'temperature'
self.darray.plot()
self.assertEqual(self.darray.name, plt.gca().get_ylabel())
def test_wrong_dims_raises_valueerror(self):
twodims = DataArray(easy_array((2, 5)))
with self.assertRaises(ValueError):
twodims.plot.line()
def test_format_string(self):
self.darray.plot.line('ro')
def test_can_pass_in_axis(self):
self.pass_in_axis(self.darray.plot.line)
def test_nonnumeric_index_raises_typeerror(self):
a = DataArray([1, 2, 3], {'letter': ['a', 'b', 'c']},
dims='letter')
with self.assertRaisesRegexp(TypeError, r'[Pp]lot'):
a.plot.line()
def test_primitive_returned(self):
p = self.darray.plot.line()
self.assertTrue(isinstance(p[0], mpl.lines.Line2D))
@pytest.mark.slow
def test_plot_nans(self):
self.darray[1] = np.nan
self.darray.plot.line()
def test_x_ticks_are_rotated_for_time(self):
time = pd.date_range('2000-01-01', '2000-01-10')
a = DataArray(np.arange(len(time)), [('t', time)])
a.plot.line()
rotation = plt.gca().get_xticklabels()[0].get_rotation()
self.assertFalse(rotation == 0)
def test_slice_in_title(self):
self.darray.coords['d'] = 10
self.darray.plot.line()
title = plt.gca().get_title()
self.assertEqual('d = 10', title)
示例8: TestPlot1D
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestPlot1D(PlotTestCase):
def setUp(self):
d = [0, 1.1, 0, 2]
self.darray = DataArray(d, coords={"period": range(len(d))})
def test_xlabel_is_index_name(self):
self.darray.plot()
self.assertEqual("period", plt.gca().get_xlabel())
def test_no_label_name_on_y_axis(self):
self.darray.plot()
self.assertEqual("", plt.gca().get_ylabel())
def test_ylabel_is_data_name(self):
self.darray.name = "temperature"
self.darray.plot()
self.assertEqual(self.darray.name, plt.gca().get_ylabel())
def test_wrong_dims_raises_valueerror(self):
twodims = DataArray(easy_array((2, 5)))
with self.assertRaises(ValueError):
twodims.plot.line()
def test_format_string(self):
self.darray.plot.line("ro")
def test_can_pass_in_axis(self):
self.pass_in_axis(self.darray.plot.line)
def test_nonnumeric_index_raises_typeerror(self):
a = DataArray([1, 2, 3], {"letter": ["a", "b", "c"]})
with self.assertRaisesRegexp(TypeError, r"[Pp]lot"):
a.plot.line()
def test_primitive_returned(self):
p = self.darray.plot.line()
self.assertTrue(isinstance(p[0], mpl.lines.Line2D))
def test_plot_nans(self):
self.darray[1] = np.nan
self.darray.plot.line()
def test_x_ticks_are_rotated_for_time(self):
time = pd.date_range("2000-01-01", "2000-01-10")
a = DataArray(np.arange(len(time)), {"t": time})
a.plot.line()
rotation = plt.gca().get_xticklabels()[0].get_rotation()
self.assertFalse(rotation == 0)
def test_slice_in_title(self):
self.darray.coords["d"] = 10
self.darray.plot.line()
title = plt.gca().get_title()
self.assertEqual("d = 10", title)
示例9: test_2d_before_squeeze
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
def test_2d_before_squeeze(self):
a = DataArray(easy_array((1, 5)))
a.plot()
示例10: TestPlot
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestPlot(PlotTestCase):
def setUp(self):
self.darray = DataArray(easy_array((2, 3, 4)))
def test1d(self):
self.darray[:, 0, 0].plot()
def test_2d_before_squeeze(self):
a = DataArray(easy_array((1, 5)))
a.plot()
def test2d_uniform_calls_imshow(self):
self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot.imshow))
def test2d_nonuniform_calls_contourf(self):
a = self.darray[:, :, 0]
a.coords["dim_1"] = [2, 1, 89]
self.assertTrue(self.contourf_called(a.plot.contourf))
def test3d(self):
self.darray.plot()
def test_can_pass_in_axis(self):
self.pass_in_axis(self.darray.plot)
def test__infer_interval_breaks(self):
self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1]))
self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5], _infer_interval_breaks([0, 1, 9, 10]))
self.assertArrayEqual(
pd.date_range("20000101", periods=4) - np.timedelta64(12, "h"),
_infer_interval_breaks(pd.date_range("20000101", periods=3)),
)
@incompatible_2_6
def test_datetime_dimension(self):
nrow = 3
ncol = 4
time = pd.date_range("2000-01-01", periods=nrow)
a = DataArray(easy_array((nrow, ncol)), coords=[("time", time), ("y", range(ncol))])
a.plot()
ax = plt.gca()
self.assertTrue(ax.has_data())
def test_convenient_facetgrid(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=["y", "x", "z"])
d.coords["z"] = list("abcd")
g = d.plot(x="x", y="y", col="z", col_wrap=2, cmap="cool")
self.assertArrayEqual(g.axes.shape, [2, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d.plot(x="x", y="y", col="z", ax=plt.gca())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d[0].plot(x="x", y="y", col="z", ax=plt.gca())
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=["y", "x", "z"])
d.coords["z"] = list("abcd")
g = d.plot(x="x", y="y", col="z", col_wrap=2, cmap="cool", subplot_kws=dict(axisbg="r"))
for ax in g.axes.flat:
self.assertEqual(ax.get_axis_bgcolor(), "r")
def test_convenient_facetgrid_4d(self):
a = easy_array((10, 15, 2, 3))
d = DataArray(a, dims=["y", "x", "columns", "rows"])
g = d.plot(x="x", y="y", col="columns", row="rows")
self.assertArrayEqual(g.axes.shape, [3, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, "[Ff]acet"):
d.plot(x="x", y="y", col="columns", ax=plt.gca())
示例11: TestDiscreteColorMap
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestDiscreteColorMap(TestCase):
def setUp(self):
x = np.arange(start=0, stop=10, step=2)
y = np.arange(start=9, stop=-7, step=-3)
xy = np.dstack(np.meshgrid(x, y))
distance = np.linalg.norm(xy, axis=2)
self.darray = DataArray(distance, list(zip(("y", "x"), (y, x))))
self.data_min = distance.min()
self.data_max = distance.max()
def test_recover_from_seaborn_jet_exception(self):
pal = _color_palette("jet", 4)
self.assertTrue(type(pal) == np.ndarray)
self.assertEqual(len(pal), 4)
def test_build_discrete_cmap(self):
for (cmap, levels, extend, filled) in [("jet", [0, 1], "both", False), ("hot", [-4, 4], "max", True)]:
ncmap, cnorm = _build_discrete_cmap(cmap, levels, extend, filled)
self.assertEqual(ncmap.N, len(levels) - 1)
self.assertEqual(len(ncmap.colors), len(levels) - 1)
self.assertEqual(cnorm.N, len(levels))
self.assertArrayEqual(cnorm.boundaries, levels)
self.assertEqual(max(levels), cnorm.vmax)
self.assertEqual(min(levels), cnorm.vmin)
if filled:
self.assertEqual(ncmap.colorbar_extend, extend)
else:
self.assertEqual(ncmap.colorbar_extend, "neither")
def test_discrete_colormap_list_of_levels(self):
for extend, levels in [
("max", [-1, 2, 4, 8, 10]),
("both", [2, 5, 10, 11]),
("neither", [0, 5, 10, 15]),
("min", [2, 5, 10, 15]),
]:
for kind in ["imshow", "pcolormesh", "contourf", "contour"]:
primitive = getattr(self.darray.plot, kind)(levels=levels)
self.assertArrayEqual(levels, primitive.norm.boundaries)
self.assertEqual(max(levels), primitive.norm.vmax)
self.assertEqual(min(levels), primitive.norm.vmin)
if kind != "contour":
self.assertEqual(extend, primitive.cmap.colorbar_extend)
else:
self.assertEqual("neither", primitive.cmap.colorbar_extend)
self.assertEqual(len(levels) - 1, len(primitive.cmap.colors))
def test_discrete_colormap_int_levels(self):
for extend, levels, vmin, vmax in [
("neither", 7, None, None),
("neither", 7, None, 20),
("both", 7, 4, 8),
("min", 10, 4, 15),
]:
for kind in ["imshow", "pcolormesh", "contourf", "contour"]:
primitive = getattr(self.darray.plot, kind)(levels=levels, vmin=vmin, vmax=vmax)
self.assertGreaterEqual(levels, len(primitive.norm.boundaries) - 1)
if vmax is None:
self.assertGreaterEqual(primitive.norm.vmax, self.data_max)
else:
self.assertGreaterEqual(primitive.norm.vmax, vmax)
if vmin is None:
self.assertLessEqual(primitive.norm.vmin, self.data_min)
else:
self.assertLessEqual(primitive.norm.vmin, vmin)
if kind != "contour":
self.assertEqual(extend, primitive.cmap.colorbar_extend)
else:
self.assertEqual("neither", primitive.cmap.colorbar_extend)
self.assertGreaterEqual(levels, len(primitive.cmap.colors))
def test_discrete_colormap_list_levels_and_vmin_or_vmax(self):
levels = [0, 5, 10, 15]
primitive = self.darray.plot(levels=levels, vmin=-3, vmax=20)
self.assertEqual(primitive.norm.vmax, max(levels))
self.assertEqual(primitive.norm.vmin, min(levels))
示例12: TestPlot
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestPlot(PlotTestCase):
def setUp(self):
self.darray = DataArray(easy_array((2, 3, 4)))
def test1d(self):
self.darray[:, 0, 0].plot()
def test_2d_before_squeeze(self):
a = DataArray(easy_array((1, 5)))
a.plot()
def test2d_uniform_calls_imshow(self):
self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot.imshow))
@pytest.mark.slow
def test2d_nonuniform_calls_contourf(self):
a = self.darray[:, :, 0]
a.coords['dim_1'] = [2, 1, 89]
self.assertTrue(self.contourf_called(a.plot.contourf))
def test3d(self):
self.darray.plot()
def test_can_pass_in_axis(self):
self.pass_in_axis(self.darray.plot)
def test__infer_interval_breaks(self):
self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1]))
self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5],
_infer_interval_breaks([0, 1, 9, 10]))
self.assertArrayEqual(
pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'),
_infer_interval_breaks(pd.date_range('20000101', periods=3)))
# make a bounded 2D array that we will center and re-infer
xref, yref = np.meshgrid(np.arange(6), np.arange(5))
cx = (xref[1:, 1:] + xref[:-1, :-1]) / 2
cy = (yref[1:, 1:] + yref[:-1, :-1]) / 2
x = _infer_interval_breaks(cx, axis=1)
x = _infer_interval_breaks(x, axis=0)
y = _infer_interval_breaks(cy, axis=1)
y = _infer_interval_breaks(y, axis=0)
np.testing.assert_allclose(xref, x)
np.testing.assert_allclose(yref, y)
def test_datetime_dimension(self):
nrow = 3
ncol = 4
time = pd.date_range('2000-01-01', periods=nrow)
a = DataArray(easy_array((nrow, ncol)),
coords=[('time', time), ('y', range(ncol))])
a.plot()
ax = plt.gca()
self.assertTrue(ax.has_data())
@pytest.mark.slow
def test_convenient_facetgrid(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool')
self.assertArrayEqual(g.axes.shape, [2, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, '[Ff]acet'):
d.plot(x='x', y='y', col='z', ax=plt.gca())
with self.assertRaisesRegexp(ValueError, '[Ff]acet'):
d[0].plot(x='x', y='y', col='z', ax=plt.gca())
@pytest.mark.slow
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool',
subplot_kws=dict(axisbg='r'))
for ax in g.axes.flat:
try:
# mpl V2
self.assertEqual(ax.get_facecolor()[0:3],
mpl.colors.to_rgb('r'))
except AttributeError:
self.assertEqual(ax.get_axis_bgcolor(), 'r')
@pytest.mark.slow
def test_plot_size(self):
self.darray[:, 0, 0].plot(figsize=(13, 5))
assert tuple(plt.gcf().get_size_inches()) == (13, 5)
self.darray.plot(figsize=(13, 5))
assert tuple(plt.gcf().get_size_inches()) == (13, 5)
self.darray.plot(size=5)
assert plt.gcf().get_size_inches()[1] == 5
self.darray.plot(size=5, aspect=2)
#.........这里部分代码省略.........
示例13: TestPlot
# 需要导入模块: from xarray import DataArray [as 别名]
# 或者: from xarray.DataArray import plot [as 别名]
class TestPlot(PlotTestCase):
def setUp(self):
self.darray = DataArray(easy_array((2, 3, 4)))
def test1d(self):
self.darray[:, 0, 0].plot()
def test_2d_before_squeeze(self):
a = DataArray(easy_array((1, 5)))
a.plot()
def test2d_uniform_calls_imshow(self):
self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot.imshow))
def test2d_nonuniform_calls_contourf(self):
a = self.darray[:, :, 0]
a.coords['dim_1'] = [2, 1, 89]
self.assertTrue(self.contourf_called(a.plot.contourf))
def test3d(self):
self.darray.plot()
def test_can_pass_in_axis(self):
self.pass_in_axis(self.darray.plot)
def test__infer_interval_breaks(self):
self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1]))
self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5],
_infer_interval_breaks([0, 1, 9, 10]))
self.assertArrayEqual(pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'),
_infer_interval_breaks(pd.date_range('20000101', periods=3)))
@incompatible_2_6
def test_datetime_dimension(self):
nrow = 3
ncol = 4
time = pd.date_range('2000-01-01', periods=nrow)
a = DataArray(easy_array((nrow, ncol)),
coords=[('time', time), ('y', range(ncol))])
a.plot()
ax = plt.gca()
self.assertTrue(ax.has_data())
def test_convenient_facetgrid(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool')
self.assertArrayEqual(g.axes.shape, [2, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, '[Ff]acet'):
d.plot(x='x', y='y', col='z', ax=plt.gca())
with self.assertRaisesRegexp(ValueError, '[Ff]acet'):
d[0].plot(x='x', y='y', col='z', ax=plt.gca())
def test_subplot_kws(self):
a = easy_array((10, 15, 4))
d = DataArray(a, dims=['y', 'x', 'z'])
d.coords['z'] = list('abcd')
g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool',
subplot_kws=dict(axisbg='r'))
for ax in g.axes.flat:
self.assertEqual(ax.get_axis_bgcolor(), 'r')
def test_convenient_facetgrid_4d(self):
a = easy_array((10, 15, 2, 3))
d = DataArray(a, dims=['y', 'x', 'columns', 'rows'])
g = d.plot(x='x', y='y', col='columns', row='rows')
self.assertArrayEqual(g.axes.shape, [3, 2])
for ax in g.axes.flat:
self.assertTrue(ax.has_data())
with self.assertRaisesRegexp(ValueError, '[Ff]acet'):
d.plot(x='x', y='y', col='columns', ax=plt.gca())