Title: [Bug]: Creating sub-plots is much slower than Plotly · Issue #26162 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Creating sub-plots is much slower than Plotly · Issue #26162 · matplotlib/matplotlib
X Title: [Bug]: Creating sub-plots is much slower than Plotly · Issue #26162 · matplotlib/matplotlib
Description: Bug summary Creating sub-plots in Matplotlib is typically 4-12x slower than Plotly. This is not a bug per se, but a serious performance issue for time-critical applications such as interactive web-apps. There are several closed GitHub is...
Open Graph Description: Bug summary Creating sub-plots in Matplotlib is typically 4-12x slower than Plotly. This is not a bug per se, but a serious performance issue for time-critical applications such as interactive web-...
X Description: Bug summary Creating sub-plots in Matplotlib is typically 4-12x slower than Plotly. This is not a bug per se, but a serious performance issue for time-critical applications such as interactive web-...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/26162
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Creating sub-plots is much slower than Plotly","articleBody":"### Bug summary\n\nCreating sub-plots in Matplotlib is typically 4-12x slower than Plotly. This is not a bug per se, but a serious performance issue for time-critical applications such as interactive web-apps. There are several closed GitHub issues about the slowness of creating sub-plots that go back 6-7 years, but it's still a problem.\n\n### Code for reproduction\n\n```python\n%matplotlib inline\r\nfrom matplotlib.figure import Figure\r\nfrom plotly.subplots import make_subplots\r\nimport numpy as np\r\nimport pandas as pd\r\nimport timeit\r\n\r\nrng = np.random.default_rng()\r\n\r\nresults = []\r\n\r\nfor i in range(30):\r\n # Random number of rows and columns.\r\n rows, cols = rng.integers(low=1, high=20, size=2)\r\n \r\n # Plotly won't accept Numpy ints.\r\n rows = int(rows)\r\n cols = int(cols)\r\n\r\n # Total number of sub-plots.\r\n total = rows*cols\r\n\r\n # Timer.\r\n t1 = timeit.default_timer()\r\n\r\n # Matplotlib.\r\n fig_matplotlib = Figure()\r\n axs_matplotlib = fig_matplotlib.subplots(nrows=rows, ncols=cols)\r\n\r\n # Timer.\r\n t2 = timeit.default_timer()\r\n\r\n # Plotly.\r\n fig_plotly = make_subplots(rows=rows, cols=cols)\r\n\r\n # Timer.\r\n t3 = timeit.default_timer()\r\n \r\n # Time-usage.\r\n t_matplotlib = t2 - t1\r\n t_plotly = t3 - t2\r\n \r\n # Relative time-usage.\r\n t_relative = t_matplotlib / t_plotly\r\n\r\n # Save results.\r\n results.append(dict(rows=rows, cols=cols, total=total,\r\n t_matplotlib=t_matplotlib, t_plotly=t_plotly,\r\n t_relative=t_relative))\r\n\r\n # Show status. \r\n print(f'{rows}\\t{cols}\\t{total}\\t{t_relative:.3f}')\r\n\r\n# Convert results to Pandas DataFrame.\r\ndf_results = pd.DataFrame(results)\r\n\r\n# Plot relative time-usage.\r\ndf_results.plot(kind='scatter', x='total', y='t_relative', grid=True);\r\n\r\n# Plot individual time-usage.\r\ndf2 = df_results.set_index('total').sort_index()\r\ndf2.plot(y=['t_matplotlib', 't_plotly'], grid=True, ylabel='seconds');\n```\n\n\n### Actual outcome\n\nIn my actual application with 3 columns and 10 rows, the time-usage for Matplotlib is consistently around 1.8 seconds, but for some reason it is only around 0.5 seconds in these tests.\r\n\r\nThis plot shows the individual time-usage for Matplotlib and Plotly, where the x-axis is the total number of sub-plots (cols * rows):\r\n\r\n\r\n\r\nNote the jagged lines for the Matplotlib time-usage. We could average several runs to make the lines smoother, but the trend is clear, and the jaggedness is actually quite strange, that the time changes so much from run to run.\r\n\r\nThis plot shows the relative time-usage (Matplotlib time / Plotly time):\r\n\r\n\r\n\n\n### Expected outcome\n\nI would like it to run [like this](https://www.youtube.com/watch?v=0hFzpF_gGys) - minus the crashes, please.\r\n\n\n### Additional information\n\nThanks again for making Matplotlib! I don't want to sound ingrateful or too demanding, as this is my second GitHub issue in a few days relating to the performance of using many sub-plots in Matplotlib. But these issues are major bottle-necks in my application that take around 90% of the runtime. I also wonder if perhaps the issues are related. (See https://github.com/matplotlib/matplotlib/issues/26150)\r\n\r\nIs there a technical reason that Plotly is so much faster than Matplotlib when it comes to having sub-plots?\r\n\r\nI imagine that Matplotlib has been made by many different people over a long period of time, so perhaps it is getting hard to understand what the code is doing sometimes?\r\n\r\nPlotly runs very fast and is easy to use, but I have already made everything in Matplotlib, and I'm not even sure Plotly has all the features I need to customize the plots. So I'm hoping it would be possible to improve the speed of Matplotlib when using sub-plots.\r\n\r\nThanks!\r\n\n\n### Operating system\n\nKubuntu 22\n\n### Matplotlib Version\n\n3.7.1\n\n### Matplotlib Backend\n\nmodule://matplotlib_inline.backend_inline\n\n### Python version\n\n3.9.12\n\n### Jupyter version\n\n6.4.12 (through VSCode)\n\n### Installation\n\npip","author":{"url":"https://github.com/Hvass-Labs","@type":"Person","name":"Hvass-Labs"},"datePublished":"2023-06-21T12:14:40.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/26162/matplotlib/issues/26162"}
| 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:eec5b036-4c13-d1aa-4590-db50572fcf2a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB16:50D6E:1C3977:281C4C:6A54EB64 |
| html-safe-nonce | 18a47ed724abf05a0335135badfc2157a61f89b96f5997b9d4b992bb95f3bed2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjE2OjUwRDZFOjFDMzk3NzoyODFDNEM6NkE1NEVCNjQiLCJ2aXNpdG9yX2lkIjoiNzI2MDMyMTk4NzcyMjczNjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f46adde8abbcfcaa274296db9db6b0c53b4746e9d11c44bc418446f7ae993d5d |
| hovercard-subject-tag | issue:1767467120 |
| 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/26162/issue_layout |
| twitter:image | https://opengraph.githubassets.com/111fcdae25e4f1ea45a155c26efa62b0367084822f34aedccae535413230f812/matplotlib/matplotlib/issues/26162 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/111fcdae25e4f1ea45a155c26efa62b0367084822f34aedccae535413230f812/matplotlib/matplotlib/issues/26162 |
| og:image:alt | Bug summary Creating sub-plots in Matplotlib is typically 4-12x slower than Plotly. This is not a bug per se, but a serious performance issue for time-critical applications such as interactive web-... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Hvass-Labs |
| hostname | github.com |
| expected-hostname | github.com |
| None | eae2aab99907699a22d2c8449aa683ca74454fca87211b98f7c7b722086b2839 |
| 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 | bb1b4ffdc07c66de20ad344214f55df0aa364bd3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width