Skip to content

Added test_pcolor in test_datetime.py #27391

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/matplotlib/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,20 @@ def test_matshow(self):
fig, ax = plt.subplots()
ax.matshow(...)

@pytest.mark.xfail(reason="Test for pcolor not written yet")
@mpl.style.context("default")
def test_pcolor(self):
fig, ax = plt.subplots()
ax.pcolor(...)
base_date = datetime.date(2020, 1, 1)
dates = [base_date + datetime.timedelta(days=i) for i in range(10)]
data = np.random.rand(10, 10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to use randomness, you will need to set a seed (we use 19680801 for tests) or else the test image will change on each run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, will add this seed to ensure the image does not change in each run.

data = np.sort(data, axis=0)
fig, ax = plt.subplots()
c = ax.pcolor(data, cmap='viridis')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no use of dates in this call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both axes of pcolor require that I pass numerical data, how do you suggest I use dates? I can use datetime in this call, but then I would have to use date2num, which defies the purpose. Not sure how else to implement. This is what I found online:

'In my understanding, you would like to have date formatted tick labels for axes when using 'pcolor'. You cannot directly provide 'pcolor' with datetime vectors as of now.
However, there is a way to use the ' datenum ' and ' datetick ' functions to get datetime axes tick labels as illustrated in the example below:
%Creating two example vectors of datetimes, tx and ty
t1 = datetime(2017,9,2,0,0,0);
t2 = datetime(2017,9,30,0,0,0);
tx = t1:5:t2;
ty = tx + day(15);
%Creating an example C matrix for pcolor plot
C = magic(length(tx));
% Using datenum to convert to real numbers for plotting and dateticks for
% date labelling. You can play around with datetick options to suit your needs
pcolor(datenum(tx), datenum(ty), C);
datetick('y', 'dd-mm-yy', 'keepticks');
datetick('x', 'dd-mm-yy', 'keepticks');'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X and Y in fact, CAN take date info directly, no need to call date2num...

>>> tx = ty = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
>>> tx, ty = np.meshgrid(tx, ty)
>>> c = np.arange(tx.size).reshape(tx.shape)
>>> plt.pcolor(tx, ty, c)

yields
test

While I made no effort to clean up the labels so they were visible/not overlapping, the point stands that they are properly converted as desired/as is the point of these tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer you found is Matlab code about Matlab's plotting, which matplotlib was originally inspired by, but has diverged from significantly since then.

ax.set_title('Pseudocolor Plot with Datetime Data')
ax.set_xticks(range(len(dates)))
ax.set_xticklabels([d.strftime('%Y-%m-%d') for d in dates], rotation=45)
fig.colorbar(c, ax=ax)
fig.tight_layout()
plt.close(fig)

@pytest.mark.xfail(reason="Test for pcolorfast not written yet")
@mpl.style.context("default")
Expand Down