Title: Fix limits if one axis scale is 'log' · Issue #27085 · matplotlib/matplotlib · GitHub
Open Graph Title: Fix limits if one axis scale is 'log' · Issue #27085 · matplotlib/matplotlib
X Title: Fix limits if one axis scale is 'log' · Issue #27085 · matplotlib/matplotlib
Description: Problem I am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simply omitted when plotting (and this is wha...
Open Graph Description: Problem I am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simpl...
X Description: Problem I am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simpl...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/27085
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fix limits if one axis scale is 'log'","articleBody":"### Problem\r\n\r\nI am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simply omitted when plotting (and this is what I want) but the y-axis limits are not updated for these missing values. It would be helpful to automatically set new axis limits if there are nans in the data. \r\n\r\nExample:\r\n\r\n```\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nx = np.arange(-10, 11, 1) \r\nplt.plot(x,x, marker='d')\r\n```\r\n\r\n\r\nand when I add the log scale on the x-axis, the negative values are omitted but the y-axis is not updated (even though I set nonpositive to 'clip' -- so maybe this is a bug?)\r\n```\r\nx = np.arange(-10, 11, 1) \r\nplt.plot(x,x, marker='d')\r\nplt.xscale('log', nonpositive='clip')\r\n```\r\n\r\n\r\nIn order to manually set `ylim`one would need to know the exact values of the data, so I suggest this could be adjusted automatically.\r\n\r\n\r\n### Setup\r\nPython 3.8.8\r\nMatplotlib 3.7.1\r\nNumpy 1.24.3\r\nnotebook 6.2.0 (executing and plotting in jupyter notebook)\r\non macOS 12.6.2 (Monterey)\r\n\r\n\r\n### Proposed solution\r\n\r\nIdea: \r\nIf the xscale is set to 'log' and there are negative values in x, take the position of minimum of x \u003c 0 and set y value at that position as new limit (ideally with the same margin as in the rest of the figure):\r\n\r\n\r\nassuming self is the axis\r\n```\r\nmargin = 0.1\r\nif len(x[x\u003c0]) !=0 and self.get_xscale=='log':\r\n ii_x_g0 = np.min(np.where(x\u003e0))\r\n self.set_ylim(bottom = y[ii_x_g0] - y[ii_x_g0] * margin)\r\n\r\n```\r\n","author":{"url":"https://github.com/swagner-astro","@type":"Person","name":"swagner-astro"},"datePublished":"2023-10-13T23:56:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/27085/matplotlib/issues/27085"}
| 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:9bcc336b-24cf-f0f4-9175-44eebbab5654 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CF72:18E786:5396F8:723418:6A52508D |
| html-safe-nonce | e7691eeac31e6c7e436535223d484805afc6700af5e76dede87f304529b26b80 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjcyOjE4RTc4Njo1Mzk2Rjg6NzIzNDE4OjZBNTI1MDhEIiwidmlzaXRvcl9pZCI6IjgxOTk2MzA5ODI2NTMwMzg3MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1402b4cdc5a0ad742345bc714137eb756e1df061f57aff97b4372a71dfa1a52a |
| hovercard-subject-tag | issue:1942790890 |
| 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/27085/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dd909f105619551de980124df55f32620723b2208fb00805e68f42f14349496c/matplotlib/matplotlib/issues/27085 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dd909f105619551de980124df55f32620723b2208fb00805e68f42f14349496c/matplotlib/matplotlib/issues/27085 |
| og:image:alt | Problem I am plotting my data (x,y) and want to show the x-axis in logscale. Because there are some negative values in x, the log10 can not be computed for all. This results in nans which are simpl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | swagner-astro |
| 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