pydata / pydata/xarray

Support for matplotlib mosaic using variable names

Open
#6,922 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

This is not related to any problem, but I think it would be nice to have a support for giving a matplotlib mosaic with the keys for the variables you want to plot for different panels and xarray parse that into the figure.

Describe the solution you'd like

Something like

import matplotlib.pyplot as plt
import xarray as xr
import numpy as np

n = 200
t = np.linspace(0,3*2*np.pi,n)
ds = xr.Dataset({letter:(("s","t"),np.sin(t)+0.5*np.random.randn(3,n)) for letter in "A B C D E".split()})
ds = ds.assign_coords(t=t,s=range(3))

mosaic = [
    ["A","A","B","B","C","C"],
    ["X","D","D","E","E","X"],
]

kw = dict(x="t",hue="s",add_legend=False)
ds.plot.line(mosaic=mosaic,empty_sentinel="X",**kw)



image

Describe alternatives you've considered

I have a code snippet that generate similar results but with more code.

import matplotlib.pyplot as plt
import xarray as xr
import numpy as np

n = 200
t = np.linspace(0,3*2*np.pi,n)
ds = xr.Dataset({letter:(("s","t"),np.sin(t)+0.5*np.random.randn(3,n)) for letter in "A B C D E".split()})
ds = ds.assign_coords(t=t,s=range(3))


mosaic = [
    ["A","A","B","B","C","C"],
    ["X","D","D","E","E","X"],
]

kw = dict(x="t",hue="s",add_legend=False)
fig = plt.figure(constrained_layout=True,figsize=(8,4))
ax = fig.subplot_mosaic(mosaic,empty_sentinel="X")
for key in ds:
    ds[key].plot.line(ax=ax[key],**kw)

image

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ds.plot.line entry point and compare the requested API with matplotlib's Figure.subplot_mosaic behavior shown in the examples. Check how dataset variables are mapped to axes and how empty_sentinel is handled. Done means a mosaic can route each named variable to its panel while preserving the existing line-plot options and behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, python
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.