Title: broken_barh appears not to work with datetime/timedelta objects · Issue #12862 · matplotlib/matplotlib · GitHub
Open Graph Title: broken_barh appears not to work with datetime/timedelta objects · Issue #12862 · matplotlib/matplotlib
X Title: broken_barh appears not to work with datetime/timedelta objects · Issue #12862 · matplotlib/matplotlib
Description: Bug report Bug summary The Axis.broken_barh function takes an xranges parameter described as "The x-positions and extends of the rectangles". But if I'm plotting datetime values on the axis, it appears impossible to supply an extent valu...
Open Graph Description: Bug report Bug summary The Axis.broken_barh function takes an xranges parameter described as "The x-positions and extends of the rectangles". But if I'm plotting datetime values on the axis, it app...
X Description: Bug report Bug summary The Axis.broken_barh function takes an xranges parameter described as "The x-positions and extends of the rectangles". But if I'm plotting datetime values on th...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/12862
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"broken_barh appears not to work with datetime/timedelta objects","articleBody":"\u003c!--To help us understand and resolve your issue, please fill out the form to the best of your ability.--\u003e\r\n\u003c!--You can feel free to delete the sections that do not apply.--\u003e\r\n\r\n### Bug report\r\n\r\n**Bug summary**\r\n\r\nThe `Axis.broken_barh` function takes an `xranges` parameter described as \"The x-positions and extends of the rectangles\". But if I'm plotting datetime values on the axis, it appears impossible to supply an extent value of a type that the function can deal with.\r\n\r\nUsing `datetime.timedelta` raises an exception `AttributeError: 'datetime.timedelta' object has no attribute 'toordinal'`, as does using an integer. Using a `datetime` value causes the bar to extend too far (which I guess isn't surprising, as `toordinal` on a datetime will *not* give a duration).\r\n\r\n**Code for reproduction**\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\nfrom datetime import datetime, timedelta\r\n\r\nlo = datetime.fromisoformat(\"2018-11-09 20:00\")\r\nhi = datetime.fromisoformat(\"2018-11-09 22:00\")\r\n\r\nfig, ax = plt.subplots()\r\n\r\nax.set_xlim(lo, hi)\r\nax.set_ylim(0, 30)\r\nax.broken_barh([(lo, timedelta(hours=1))], (10, 20))\r\n\r\nplt.show()\r\n```\r\n\r\n**Actual outcome**\r\n\r\n```\r\nTraceback (most recent call last):\r\n File \".\\problem.py\", line 11, in \u003cmodule\u003e\r\n ax.broken_barh([(lo, timedelta(hours=1))], (10, 20))\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\__init__.py\", line 1810, in inner\r\n return func(ax, *args, **kwargs)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\axes\\_axes.py\", line 2528, in broken_barh\r\n xranges = self.convert_xunits(xranges)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\artist.py\", line 186, in convert_xunits\r\n return ax.xaxis.convert_units(x)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\axis.py\", line 1530, in convert_units\r\n ret = self.converter.convert(x, self.units, self)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\dates.py\", line 1801, in convert\r\n return date2num(value)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\dates.py\", line 428, in date2num\r\n return _to_ordinalf_np_vectorized(d)\r\n File \"C:\\...\\lib\\site-packages\\numpy\\lib\\function_base.py\", line 1972, in __call__\r\n return self._vectorize_call(func=func, args=vargs)\r\n File \"C:\\...\\lib\\site-packages\\numpy\\lib\\function_base.py\", line 2048, in _vectorize_call\r\n outputs = ufunc(*inputs)\r\n File \"C:\\...\\lib\\site-packages\\matplotlib\\dates.py\", line 229, in _to_ordinalf\r\n base = float(dt.toordinal())\r\nAttributeError: 'datetime.timedelta' object has no attribute 'toordinal'\r\n```\r\n\r\n**Expected outcome**\r\n\r\nA graph showing a single bar from 20:00 to 21:00.\r\n\r\nIf this is a limitation of the current version of matplotlib, I'd be perfectly happy with a workaround that gave the expected result by another means (but the approaches I've tried have resulted in the axis tick marks not being correctly formatted as times).\r\n\r\n**Matplotlib version**\r\n * Operating system: Windows 7\r\n * Matplotlib version: 3.0.2\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): TkAgg\r\n * Python version: 3.7.1\r\n * Jupyter version (if applicable): N/A\r\n * Other libraries: None\r\n\r\nPython installed from the python.org Windows 64-bit installer. Matplotlib installed from the wheel on PyPI (`pipenv install matplotlib`).","author":{"url":"https://github.com/pfmoore","@type":"Person","name":"pfmoore"},"datePublished":"2018-11-21T15:31:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/12862/matplotlib/issues/12862"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:923ba9be-91cf-7773-bc72-7ef75ed756b0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA5A:2B32D1:4DEE7:65ED5:6A52D8C7 |
| html-safe-nonce | 9bed5bf5fa83e5854f08679ddf752a5e0df53b346a0616c9b1f4467f9ffb5552 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTVBOjJCMzJEMTo0REVFNzo2NUVENTo2QTUyRDhDNyIsInZpc2l0b3JfaWQiOiI1MTU0NzA0NzA5NzQzOTkwOTgzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 582312ce65f360a64321f9c63618fdb517de952986baa59cbf80031922f4f194 |
| hovercard-subject-tag | issue:383171580 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/12862/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6ee78ed6d200de2594e04b8bc30554f51ce8cc5dc62d936cef37dc2cb463087/matplotlib/matplotlib/issues/12862 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6ee78ed6d200de2594e04b8bc30554f51ce8cc5dc62d936cef37dc2cb463087/matplotlib/matplotlib/issues/12862 |
| og:image:alt | Bug report Bug summary The Axis.broken_barh function takes an xranges parameter described as "The x-positions and extends of the rectangles". But if I'm plotting datetime values on the axis, it app... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pfmoore |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width