pydata / pydata/xarray

inconsistency between resample and plotting

Open
#6,677 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What is your issue?

I believe there is an inconsistency between using the resample function (left labeled) and the plotting function (center labeled).
Maybe the example below help illustrating the issue:

d = [
    [1,0,0,0,0,0,1,0,0,0,0,0],
    [0,0,1,1,0,0,0,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0,0,1,0],
]

t = pd.date_range('2000-01-01 00:05','2000-01-01 00:17',freq='1t',closed='left')

da = xr.DataArray(d,dims=['i','t'],coords={'t':t})

f,axs = plt.subplots(4,sharex=True,constrained_layout=True)

def _plot(da_,ax_):
    return da_.plot(ax=ax_,add_colorbar=False,vmin=0,vmax=1.001,levels=11)

for ax,tt in zip(axs[1:],['2t','3t','4t']):
    da_ = da.resample({'t':tt}).mean()
    gr = _plot(da_,ax)
    ax.set_title(f'resample: {tt}')

_plot(da,axs[0])
axs[0].set_title('orginal')
    

for ax in axs:
    ax.grid()
    ax.set_xticks(t)
    ax.set_xticklabels(t.strftime('%M'))
    ax.set_xlim(pd.to_datetime('2000-01-01 00:00'),pd.to_datetime('2000-01-01 00:20'))
    
f.colorbar(gr,ax=axs) 

Screenshot 2022-06-09 at 0 37 44

In the example above, the most relevant problem is the high value at min 11 in panel 1 that after resampling to 4 minutes in panel 4 gets shifted and displayed between minutes 6 and 10.
I know that i can shift the results from resample with the parameter loffset (='30s' ). but this that not help since now the high value in panel 1 (min 11) is also shifted to minutes 6-10 in panel 4

d = [
    [1,0,0,0,0,0,1,0,0,0,0,0],
    [0,0,1,1,0,0,0,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0,0,1,0],
]


t = pd.date_range('2000-01-01 00:05','2000-01-01 00:17',freq='1t',closed='left')

da = xr.DataArray(d,dims=['i','t'],coords={'t':t})

f,axs = plt.subplots(4,sharex=True,constrained_layout=True)

def _plot(da_,ax_):
    return da_.plot(ax=ax_,add_colorbar=False,vmin=0,vmax=1.001,levels=11)

for ax,tt in zip(axs[1:],['2t','3t','4t']):
    da_ = da.resample({'t':tt},loffset='30s').mean()
    gr = _plot(da_,ax)
    ax.set_title(f'resample: {tt}')

_plot(da,axs[0])
axs[0].set_title('orginal')
    

for ax in axs:
    ax.grid()
    ax.set_xticks(t)
    ax.set_xticklabels(t.strftime('%M'))
    ax.set_xlim(pd.to_datetime('2000-01-01 00:00'),pd.to_datetime('2000-01-01 00:20'))
    
f.colorbar(gr,ax=axs) 

Screenshot 2022-06-09 at 0 38 42

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 by running the issue's reproducible example with xarray DataArray.resample and plot, then compare the resampled coordinates with the plotted positions for the 2t, 3t, and 4t cases. The work is done when the resampling and plotting behavior is made consistent or the expected coordinate behavior is clearly documented, with coverage for the demonstrated case.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data, data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.