Xarray离散散点图:指定图例/颜色顺序

发布于 2025-01-27 05:47:59 字数 1080 浏览 4 评论 0原文

dataSet dataSet xr.plot.scatter()在其中产生传奇离散值是任意排序的,对应于每个级别的不可预测的颜色分配。是否可以为给定离散值指定特定的颜色或位置?

一个简单的可重复的示例:

import xarray as xr

# get a predefined dataset
uvz = xr.tutorial.open_dataset("eraint_uvz")

# select a 2-D subset of the data
uvzr = uvz.isel(level=0, month=0, latitude=slice(150, 242),
                longitude=slice(240, 300))

# define a discrete variable based on levels of a continuous variable
uvzr['zone'] = 'A'
uvzr['zone'] = uvzr.zone.where(uvzr.u > 30, other='C')
uvzr['zone'] = uvzr.zone.where(uvzr.u > 10, other='B')

# do the plot
xr.plot.scatter(uvzr, x='longitude', y='latitude', hue='zone')

“这会产生以下图”

有没有办法确保从上到下安排传奇条目'a'''','a','b','c',说?还是确保将a分配给蓝色,而b则为橙色?

我知道我可以重置matplotlib颜色循环器的值,但是为此,我首先需要知道将绘制哪个离散值。

我使用xarray v2022 .3.0在Python 3.8.6上。借助Xarray的较早版本(我认为0.16),将级别按字母顺序排列。

Plotting a discrete xarray DataArray variable in a Dataset with xr.plot.scatter() yields a legend in which the discrete values are ordered arbitrarily, corresponding to unpredictable colour assignment to each level. Would it be possible to specify a specific colour or position for a given discrete value?

A simple reproducible example:

import xarray as xr

# get a predefined dataset
uvz = xr.tutorial.open_dataset("eraint_uvz")

# select a 2-D subset of the data
uvzr = uvz.isel(level=0, month=0, latitude=slice(150, 242),
                longitude=slice(240, 300))

# define a discrete variable based on levels of a continuous variable
uvzr['zone'] = 'A'
uvzr['zone'] = uvzr.zone.where(uvzr.u > 30, other='C')
uvzr['zone'] = uvzr.zone.where(uvzr.u > 10, other='B')

# do the plot
xr.plot.scatter(uvzr, x='longitude', y='latitude', hue='zone')

This produces the following plot

Is there a way to ensure that the legend entries are arranged 'A', 'B', 'C' from top to bottom, say? Or ensure that A is assigned to blue, and B to orange, for example?

I know I can reset the values of the matplotlib color cycler, but for that to be useful I first need to know which order the discrete values will be plotted in.

I'm using xarray v2022.3.0 on python 3.8.6. With an earlier version of xarray (I think 0.16) the levels were arranged alphabetically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2025-02-03 05:47:59

我使用xarray.dataset.stackxr.where(...,drop = true)> xray.dataset.stack

import numpy as np   # for unique, to cycle through values
import matplotlib.pyplot as plt   # to get a legend

# instead of np.unique you could pass an iterable of your choice
# specifying the order
for value in np.unique(uvzr.zone):
    # convert to a 1-D dataframe with a co-ordinate including all
    # unique combinations of latitude-longitude values
    uvzr_stacked = uvzr.stack({'location':('longitude', 'latitude')})

    # now select only those grid points in zone value
    uvzr_stacked = uvzr_stacked.where(uvzr_stacked.zone == value,
                                      drop=True)

    # the plotting function can't see the original dims any more;
    # a new name is required, however
    uvzr_stacked['lat'] = uvzr_stacked.latitude
    uvzr_stacked['lon'] = uvzr_stacked.longitude

    # plot!
    xr.plot.scatter(uvzr_stacked, x='lon', y='lat', hue='zone',
                    add_guide=False)

plt.legend(title='zone')

I found an ugly workaround using xarray.Dataset.stack and xr.where(..., drop=True), in case anyone else is stuck with a similar problem.

import numpy as np   # for unique, to cycle through values
import matplotlib.pyplot as plt   # to get a legend

# instead of np.unique you could pass an iterable of your choice
# specifying the order
for value in np.unique(uvzr.zone):
    # convert to a 1-D dataframe with a co-ordinate including all
    # unique combinations of latitude-longitude values
    uvzr_stacked = uvzr.stack({'location':('longitude', 'latitude')})

    # now select only those grid points in zone value
    uvzr_stacked = uvzr_stacked.where(uvzr_stacked.zone == value,
                                      drop=True)

    # the plotting function can't see the original dims any more;
    # a new name is required, however
    uvzr_stacked['lat'] = uvzr_stacked.latitude
    uvzr_stacked['lon'] = uvzr_stacked.longitude

    # plot!
    xr.plot.scatter(uvzr_stacked, x='lon', y='lat', hue='zone',
                    add_guide=False)

plt.legend(title='zone')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文