Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

rawwash
Copy link
Contributor

@rawwash rawwash commented Nov 29, 2023

PR summary

I have added a datetime smoke test for Axes.pcolor in lib/matplotlib/test/test_datetime.py.
This addresses the Axes.pcolor task from #26864.
pcolor test

PR checklist

Comment on lines 404 to 410
data = np.random.rand(10, 10)
fig, ax = plt.subplots()
ax.pcolor(...)
c = ax.pcolor(data)
fig.colorbar(c, ax=ax)
ax.set_title('Pseudocolor Plot Test')
assert c is not None, "Failed to create pcolor plot"
plt.close(fig)
Copy link
Member

Choose a reason for hiding this comment

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

This does not use datetime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is like a heat map, and we are only inputting integer data, do you think we should keep this test?

Copy link
Member

Choose a reason for hiding this comment

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

Unlike spy, you can supply x and y values to pcolor, and those are the ones that should be given datetimes here.

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolor.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changes complete!

QuLogic
QuLogic previously approved these changes Nov 30, 2023
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.random.rand(10, 10)
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.

@QuLogic QuLogic dismissed their stale review November 30, 2023 11:46

I don't think I intended to approve this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants