Skip to content
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

Dataset plot line #4235

Open
DancingQuanta opened this issue Jul 17, 2020 · 5 comments · May be fixed by #4820
Open

Dataset plot line #4235

DancingQuanta opened this issue Jul 17, 2020 · 5 comments · May be fixed by #4820

Comments

@DancingQuanta
Copy link

Is your feature request related to a problem? Please describe.

Dataset have one plotting method Dataset.plot.scatter. This uses matplotlb's ax.scatter which have a limited presentation configuration (eg linestyle and marker).
I have 2D X and Y data which I like to plot against each other. The DataArray.plot.line works well if I only use a 1D X data.

Describe the solution you'd like

A new plotting method Dataset.plot.line based on matplotlib's ax.plot which will allow me to plot 2D X and Y against each other with flexible presentation configuration. In other to be able to produce 1D lines, this must demand a required 3D dimension as an argument (eg a choice of hue, col or others).

I suspect that as increase in number of dimensions from 1D to 2D for both variables and later to ND, there will be many ways to interpret the arguments given to the plotting function,. However, the fact the method name is line in indicate that it must produce 1D lines, and use other dimensions to create other features such as legend or subplots.

Having seen the code for the Dataset.plot.scatter I am keen to submit a PR to add Dataset.plot.line.

@DancingQuanta DancingQuanta changed the title A general dataset plot Dataset plot line Jul 17, 2020
@dcherian
Copy link
Contributor

Sounds good to me.

cc @mathause @yohai

@mathause
Copy link
Collaborator

To be sure - you want to get the x-values from one DataArray and the y-values from another? Similarly to http://xarray.pydata.org/en/stable/plotting.html#datasets but drawing lines instead of scatterpoints?

Sounds reasonable but may be a bit more difficult than scatter because you cannot just flatten your array (I think):

ax.scatter(
data["x"].where(mask, drop=True).values.flatten(),
data["y"].where(mask, drop=True).values.flatten(),

@yohai
Copy link
Contributor

yohai commented Jul 19, 2020

Sounds good. Just to mention that it might be easy to implement by stacking the arrays using .to_array and then just using DataArray.plot.line.

@DancingQuanta
Copy link
Author

DancingQuanta commented Jul 23, 2020

To give a bit more context, an example code simulating an experimental measurement.

import numpy as np
import xarray as xr
from scipy.stats import norm
from matplotlib import pyplot as plt

# nominal x
x = xr.DataArray(np.arange(-10, 10, 0.1), dims='index', name='x')

# Shift loc
loc = xr.DataArray(np.arange(-0.5, 1, 0.5), dims='loc', name='loc')

# Number of experiments
exp = xr.DataArray(range(3), dims='exp', name='exp')

# Add noise to x per experiment
noise = xr.DataArray(np.random.rand(len(x), len(loc)),
                     coords={'loc': loc},
                     dims=['index', 'loc'])
x = x + noise * 0.5

# Measure
y = xr.apply_ufunc(
        norm.pdf, x, x['loc'], 1,
        input_core_dims=[['index'], [], []],
        output_core_dims=[['index']],
        vectorize=True
    )

# Name
x.name = 'x'
y.name = 'y'

# Merge
data = xr.merge([x, y])

I wish to be able to use this

# Plot y against x
data.plot.line(x='x', y='y', hue='loc')

However, the closest I could get is with only y

y.plot.line(x='index', hue='loc')

@DancingQuanta DancingQuanta reopened this Jul 23, 2020
@Illviljan Illviljan linked a pull request Jan 17, 2021 that will close this issue
5 tasks
@stale
Copy link

stale bot commented Apr 17, 2022

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Apr 17, 2022
@Illviljan Illviljan removed the stale label Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants