Title: cbook._reshape_2D flattens ndarray with 2 dims (rectangular ndarray) · Issue #8092 · matplotlib/matplotlib · GitHub
Open Graph Title: cbook._reshape_2D flattens ndarray with 2 dims (rectangular ndarray) · Issue #8092 · matplotlib/matplotlib
X Title: cbook._reshape_2D flattens ndarray with 2 dims (rectangular ndarray) · Issue #8092 · matplotlib/matplotlib
Description: Bug report boxplot throwing an error (below) when x is an ndarray with len(x.shape)==2 (I.E. when x is rectangular). ValueError: List of boxplot statistics and 'positions' values must have same the length Code for reproduction import num...
Open Graph Description: Bug report boxplot throwing an error (below) when x is an ndarray with len(x.shape)==2 (I.E. when x is rectangular). ValueError: List of boxplot statistics and 'positions' values must have same the...
X Description: Bug report boxplot throwing an error (below) when x is an ndarray with len(x.shape)==2 (I.E. when x is rectangular). ValueError: List of boxplot statistics and 'positions' values must have ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/8092
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"cbook._reshape_2D flattens ndarray with 2 dims (rectangular ndarray)","articleBody":"### Bug report\r\n\r\nboxplot throwing an error (below) when x is an ndarray with len(x.shape)==2 (I.E. when x is rectangular).\r\n\r\n**`ValueError: List of boxplot statistics and 'positions' values must have same the length`**\r\n\r\n**Code for reproduction**\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\na = np.array([np.array([1, 2, 3, 4]), np.array([3, 2, 7, 4]), np.array([3, 9, 3, 1, 6])]) \r\nb = np.array([np.array([1, 2, 3, 4]), np.array([3, 2, 7, 4]), np.array([3, 9, 3, 1])]) \r\n\r\n# Ragged ndarray works correctly\r\nplt.boxplot(x=a, positions=range(len(a)))\r\n\r\n# Rectangular ndarray throws error (above)\r\nplt.boxplot(x=b, positions=range(len(b)))\r\n```\r\n\r\n**Matplotlib version**\r\n\r\n- matplotlib 1.5.3 np111py35_0\r\n- python 3.5.2\r\n- Windows Server 2012 R2 Standard\r\n- conda installation\r\n\r\n\r\n**Possible cause**\r\n\r\nI believe the issue is in cbook._reshape_2D in the line below. I am not sure why that logic is in place but assume it is for good reason.\r\n```python \r\nX = [X[:, i] for i in xrange(ncols)]\r\n```\r\n\r\nGot there by looking in:\r\n\r\n1. _axes.boxplot\r\n2. _axes.bxp\r\n3. cbook.boxplot_stats\r\n\r\nreference _reshape_2D:\r\n\r\n```python\r\ndef _reshape_2D(X):\r\n \"\"\"\r\n Converts a non-empty list or an ndarray of two or fewer dimensions\r\n into a list of iterable objects so that in\r\n\r\n for v in _reshape_2D(X):\r\n\r\n v is iterable and can be used to instantiate a 1D array.\r\n \"\"\"\r\n if hasattr(X, 'shape'):\r\n # one item\r\n if len(X.shape) == 1:\r\n if hasattr(X[0], 'shape'):\r\n X = list(X)\r\n else:\r\n X = [X, ]\r\n\r\n # several items\r\n elif len(X.shape) == 2:\r\n nrows, ncols = X.shape\r\n if nrows == 1:\r\n X = [X]\r\n elif ncols == 1:\r\n X = [X.ravel()]\r\n else:\r\n X = [X[:, i] for i in xrange(ncols)]\r\n else:\r\n raise ValueError(\"input `X` must have 2 or fewer dimensions\")\r\n\r\n if not hasattr(X[0], '__len__'):\r\n X = [X]\r\n else:\r\n X = [np.ravel(x) for x in X]\r\n\r\n return X\r\n```\r\n\r\n**Current Workaround**\r\nConverting the ndarray to a list of lists. The necessity for this workaround doesn't really make sense especially since the plotting works fine with a ragged/non-rectangular ndarray but does not work with a rectangular ndarray.","author":{"url":"https://github.com/lcapalleja","@type":"Person","name":"lcapalleja"},"datePublished":"2017-02-16T23:49:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":19},"url":"https://github.com/8092/matplotlib/issues/8092"}
| 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:969906bd-9bea-dd1a-bf3b-7b0ca4de4978 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB84:C1205:131711:1A0C14:6A52397A |
| html-safe-nonce | 22d430464087b1347cc38154f5b0630446ceda232396f0384c7a903340210fca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjg0OkMxMjA1OjEzMTcxMToxQTBDMTQ6NkE1MjM5N0EiLCJ2aXNpdG9yX2lkIjoiNDM5NTk5NDU5NjU1NzA3Njg1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2ef7816c096c0dbe82a059c6addb6f8251c2785cd3b5161e4bf5170eac81f1e7 |
| hovercard-subject-tag | issue:208286644 |
| 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/8092/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5d51f9a1705b519b74e93a500e98845622863095201248b709cdadf7d50ad01e/matplotlib/matplotlib/issues/8092 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5d51f9a1705b519b74e93a500e98845622863095201248b709cdadf7d50ad01e/matplotlib/matplotlib/issues/8092 |
| og:image:alt | Bug report boxplot throwing an error (below) when x is an ndarray with len(x.shape)==2 (I.E. when x is rectangular). ValueError: List of boxplot statistics and 'positions' values must have same the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lcapalleja |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width