Xarray离散散点图:指定图例/颜色顺序
用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')
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用
xarray.dataset.stack
和xr.where(...,drop = true)
> xray.dataset.stackI found an ugly workaround using
xarray.Dataset.stack
andxr.where(..., drop=True)
, in case anyone else is stuck with a similar problem.