-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor line plotting #4866
base: main
Are you sure you want to change the base?
Refactor line plotting #4866
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it might be for a future PR but I think lineplots should support colorbars and linewidth:
Line plot with linewidth option and colorbar
def _lineplot(ds, x, y, hue, linewidth):
if len(ds[y].dims) > 3:
raise NotImplementedError("too many dims.")
fig, ax = plt.subplots(1, 1)
for i, width in enumerate(ds[linewidth]):
# Filter along linewidth:
dsi = ds.isel(**{linewidth: i})
# Values to plot:
xplt = dsi[x]
# if xplt has no dims:
(xdim,) = xplt.dims
(huedim,) = dsi[hue].dims
yplt = dsi[y].transpose(..., xdim, huedim)
# Set plot properties:
len_lines = len(ds[hue])
cmap = plt.get_cmap("viridis", len_lines)
colors = plt.cycler(color=cmap(np.arange(len_lines)))
lw = plt.cycler(lw=[1 + i * 3])
ax.set_prop_cycle(colors * lw)
# p = xr.broadcast(xplt, yplt)
plt.plot(xplt, yplt, label=width.values)
plt.legend()
# ax.plot doesn't return a mappable that fig.colorbar can parse. Create
# one and return that one instead:
norm = plt.Normalize(vmin=ds[hue].min(), vmax=ds[hue].max())
primitive = plt.cm.ScalarMappable(cmap=plt.get_cmap("viridis"), norm=norm)
fig.colorbar(mappable=primitive)
return fig, ax
ds = xr.tutorial.scatter_example_dataset()
ds1 = ds.sel(z=0)
_lineplot(ds=ds1, x="y", y="A", hue="w", linewidth="x")
I've been trying to move ds.plot.scatter to the dataarray side so I have been playing around with a similar decorator as your _plot1d. It proved quite tricky for me to make the different primitives fit similar code. This one is a bit different and maybe it bypasses some of those issues I had. Just something to keep in mind.
Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on merging just after the release - although I must say I don't do a lot of plotting with master. pity the signatures are different.
Refactors line plotting to use a
_plot1d
decorator.Next i'll use this decorator on
hist
so we can "facet" and "hue" histograms.see #4288