Added test_pcolor in test_datetime.py#27391
Conversation
| 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) |
There was a problem hiding this comment.
Since this is like a heat map, and we are only inputting integer data, do you think we should keep this test?
There was a problem hiding this comment.
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
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
There is no use of dates in this call.
There was a problem hiding this comment.
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');'
There was a problem hiding this comment.
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)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.
There was a problem hiding this comment.
The answer you found is Matlab code about Matlab's plotting, which matplotlib was originally inspired by, but has diverged from significantly since then.
I don't think I intended to approve this yet.

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.
PR checklist