Title: Bar plot of pandas Series requires first index value to be 0 · Issue #16387 · matplotlib/matplotlib · GitHub
Open Graph Title: Bar plot of pandas Series requires first index value to be 0 · Issue #16387 · matplotlib/matplotlib
X Title: Bar plot of pandas Series requires first index value to be 0 · Issue #16387 · matplotlib/matplotlib
Description: Bug report Bug summary When trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a TypeError: only size-1 arrays can be converted to Python scalars exception when the bottom argument is given...
Open Graph Description: Bug report Bug summary When trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a TypeError: only size-1 arrays can be converted to Python scalars exc...
X Description: Bug report Bug summary When trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a TypeError: only size-1 arrays can be converted to Python scalars exc...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/16387
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bar plot of pandas Series requires first index value to be 0","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n\r\nWhen trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a `TypeError: only size-1 arrays can be converted to Python scalars` exception when the `bottom` argument is given. When `bottom` is not given, bar width is not uniform as expected.\r\n\r\n**Code for reproduction**\r\n\r\n\u003c!--A minimum code snippet required to reproduce the bug.\r\nPlease make sure to minimize the number of dependencies required, and provide\r\nany necessary plotted data.\r\nAvoid using threads, as Matplotlib is (explicitly) not thread-safe.--\u003e\r\n\r\n```python\r\nimport pandas as pd\r\nimport matplotlib.pyplot as plt\r\ndf = pd.DataFrame({'i':[1,4,4,4,6,6], 'n':[3,5,1,2,3,4]})\r\n\r\nix = df.index%2 == 0\r\nplt.figure()\r\n# produces expected plot with bars offset from x axis\r\nplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'], bottom=df.loc[ix, 'i'])\r\nplt.figure()\r\n# produces expected plot\r\nplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'])\r\n\r\nix = df.index%2 == 1\r\nplt.figure()\r\n# the following fails with `TypeError`\r\n# plt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'], bottom=df.loc[ix, 'i'])\r\n\r\n# converting to a list fixes the issue and produces the expected outcome.\r\n# note that casting both x and bottom to list is required to produce expected outcome.\r\nplt.bar(x=list(df.loc[ix,'i']), height=df.loc[ix, 'n'], bottom=list(df.loc[ix, 'i']))\r\n\r\nplt.figure()\r\n# produces plot with unexpected bar width\r\nplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'])\r\n\r\n# again, converting to list produces expected outcome\r\nplt.figure()\r\nplt.bar(x=list(df.loc[ix,'i']), height=df.loc[ix, 'n'])\r\n```\r\n\r\n**Matplotlib version**\r\n\u003c!--Please specify your platform and versions of the relevant libraries you are using:--\u003e\r\n * Operating system: macOS 10.15\r\n * Matplotlib version: 3.1.1\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): MacOSX\r\n * Python version: 3.6.5\r\n * Jupyter version (if applicable):\r\n * Other libraries: pandas 0.25.2\r\n\r\n\u003c!--Please tell us how you installed matplotlib and python e.g., from source, pip, conda--\u003e\r\n\u003c!--If you installed from conda, please specify which channel you used if not the default--\u003e\r\nInstalled from conda, using default channel.\r\n","author":{"url":"https://github.com/zeeMonkeez","@type":"Person","name":"zeeMonkeez"},"datePublished":"2020-02-01T22:45:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/16387/matplotlib/issues/16387"}
| 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:09e9acaa-aa6a-0d43-ce93-951721c5bb05 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A6C0:DB1B4:291E28:3C1617:6A57440E |
| html-safe-nonce | d320ff5182a1bd589053de8a43a09021e8436b6cba85599954f3aac9626d212d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkMwOkRCMUI0OjI5MUUyODozQzE2MTc6NkE1NzQ0MEUiLCJ2aXNpdG9yX2lkIjoiMjc2MzMyMzE0MDQ1MzM4NTIzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58c9bfc552036107b301a29a31c3d16e8a1f9f03c482e8dff3cb104417be9a30 |
| hovercard-subject-tag | issue:558609959 |
| 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/16387/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c912251068949dc43c365446a57b2c90a3168b90b11f4c3a7b571fff4ae7ff32/matplotlib/matplotlib/issues/16387 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c912251068949dc43c365446a57b2c90a3168b90b11f4c3a7b571fff4ae7ff32/matplotlib/matplotlib/issues/16387 |
| og:image:alt | Bug report Bug summary When trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a TypeError: only size-1 arrays can be converted to Python scalars exc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zeeMonkeez |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4e7a7296a3830877cf21a6ad2a972c9e618a48915e03966cf0c53eb08e5aad98 |
| 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 | 2576d1f0198cf1588faf2edf27f1ed120903be10 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width