Add test_bar in test_datetime#27033
Conversation
ksunden
left a comment
There was a problem hiding this comment.
I think the bar graphs with dates on the vertical (for bar, horizonatal for barh) axis should be given a bit more care here. That there is an implicit 0 mark, which results in 1970-01-01 is not a particularly good representation of a bar plot.
I think that for this, there should be a bottom argument specified as a datetime and then using a timedelta instead of a datetime for the heights, as indicated by the bar docstring
Here is some code I wrote for someone who had started working on this but never opened a PR:
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
import matplotlib as mpl
mpl.rcParams["date.converter"] = 'concise'
fig, (ax1, ax2) = plt.subplots(2, 1, layout='constrained')
price_date = np.array([ datetime(2020, 6, 30),
datetime(2020, 7, 22),
datetime(2020, 8, 3),
datetime(2020, 9, 14)], dtype=np.datetime64)
price_close = [8800, 2600, 8500, 7400]
start_date = np.datetime64(datetime(2020, 6, 1))
ax1.bar(price_date, price_close, width=np.timedelta64(4, "D"))
ax2.bar(np.arange(4), price_date-start_date, bottom=start_date)Note the start_date and price_date-start_date in this example.
|
Also, while in your example it is not a bad width, I may suggest also testing the |
|
Thanks for the suggestions. I'll modify the code and raise a revision. |
|
I'll be raising the revision in next 3 days. |
|
@ksunden I've raised a revision. Please review the changes. |
|
There still appear to be the implicit 1970 in the new version And it is very hard to see what is happening in especially the middle two plots (but also the top plot) as the widths overlap all of the bars. A single width is probably sufficient, we just mostly want to test it out that it handles the units as expected, so changing from the default is a good check. |
|
Noted. |
|
@ksunden |

PR summary
Version 4:
Modified tests and raised a revision
Version 4 Image output -

Version 3:
Modified tests and raised a revision
Version 3 Image output -

Version 2:
Modified tests as per this and this
Version 2 Image output -

Version 1:
Adding code for
test_barmethod intest_datetime.pymentioned in #26864Version 1 Image output -

PR checklist