Title: plotting using secondary axis with mpl_toolkits.axes_grid1.parasite_axes.SubplotHost().twin() produces very weird outputs · Issue #7258 · matplotlib/matplotlib · GitHub
Open Graph Title: plotting using secondary axis with mpl_toolkits.axes_grid1.parasite_axes.SubplotHost().twin() produces very weird outputs · Issue #7258 · matplotlib/matplotlib
X Title: plotting using secondary axis with mpl_toolkits.axes_grid1.parasite_axes.SubplotHost().twin() produces very weird outputs · Issue #7258 · matplotlib/matplotlib
Description: For several plots I'd like to have a secondary x axis that is a non-affine transformation of the original axis. Following a very old discussion on nabble I recognized that it is only possible, if I define my own matplotlib.transforms.Tra...
Open Graph Description: For several plots I'd like to have a secondary x axis that is a non-affine transformation of the original axis. Following a very old discussion on nabble I recognized that it is only possible, if I...
X Description: For several plots I'd like to have a secondary x axis that is a non-affine transformation of the original axis. Following a very old discussion on nabble I recognized that it is only possible, ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/7258
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"plotting using secondary axis with mpl_toolkits.axes_grid1.parasite_axes.SubplotHost().twin() produces very weird outputs","articleBody":"For several plots I'd like to have a secondary x axis that is a non-affine transformation of the original axis. Following a very old discussion on [nabble](http://matplotlib.1069221.n5.nabble.com/Dual-x-axes-with-transformation-td10865.html) I recognized that it is only possible, if I define my own matplotlib.transforms.Transform() class or I have to set tick locations manually according to a discussion on [stackoverflow](http://stackoverflow.com/questions/10514315/how-to-add-a-second-x-axis-in-matplotlib). I managed to make the code from the nabble link work, but I had to rename the class method transform() to transform_non_affine(). Otherwise it would not work.\nSo I now have the following code, which seemed to work fine for the first few tests.\n\n```\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.transforms as mtransforms\nfrom mpl_toolkits.axes_grid1.parasite_axes import SubplotHost\n\n\ndef transformed_x_axis(ax, transformation):\n\n class MyTransform(mtransforms.Transform):\n input_dims = 1\n output_dims = 1\n is_separable = False\n has_inverse = False\n\n def transform_non_affine(self, x):\n return transformation(x)\n\n def inverted(self):\n return MyInvertedTransform()\n\n class MyInvertedTransform(MyTransform):\n def inverted(self):\n return MyTransform()\n\n aux_trans = mtransforms.BlendedGenericTransform(\n MyTransform(), mtransforms.IdentityTransform())\n\n ax2 = ax.twin(aux_trans)\n ax2.set_viewlim_mode(\"transform\")\n ax2.axis[\"right\"].toggle(ticklabels=False)\n return ax2\n```\n\nFor convenience I defined a function 'transformed_x_axis()' that just accepts an original matplotlib.axes.Axes object and a transformation formula.\n(Actually, I think that my use case is pretty common and matplotlib should provide such kind of function that I tried to use, here.)\n\nUsing it with\n\n```\nfig = plt.figure()\nax = SubplotHost(fig, 1,1,1)\nfig.add_subplot(ax)\n\nax2 = transformed_x_axis(ax, lambda x: 1. / x)\n\nx = np.linspace(1., 2., 200)\nax.plot(x, np.sin(x))\nplt.show()\n```\n\nproduces exactly what I want -- a plot with a secondary transformed x axis:\n\n\nUnfortunately, for other transformations or for other data limits I see all kinds of weirdest things happening.\n\nFor other data ranges, e.g. `x = np.linspace(1., 5., 200)`, the ticker has problems and draws ticklabels on top of each other:\n\n\nIf the axis approaches the critical point 0.0 of my transformation, the ticks don'e even fill the axis but only a tiny part of it:\n\n\nI guess that this is a result of the linear scale of the ticklabels. It seems the ticks for the axis are chosen by value and not by position in axes coordinates, which I find kind of wrong.\n\nOther transformations also produce weird things, e.g.\n\n```\nfig = plt.figure()\nax = SubplotHost(fig, 1,1,1)\nfig.add_subplot(ax)\n\nax2 = transformed_x_axis(ax, lambda x: np.exp(x))\n\nx = np.linspace(1., 5., 200)\nax.plot(x, np.sin(x))\nplt.show()\n```\n\ngives me a RuntimeError in an IPython Notebook and in a terminal it produces a secondary axis that contains nothing but the value 0.\n\n\nFor other data limits,\n\n```\nax2 = transformed_x_axis(ax, lambda x: np.exp(x))\nx = np.linspace(1., 2., 200)\nax.plot(x, np.sin(x))\n```\n\nI get a MemoryError in the notebook. When done in the terminal, I see no secondary x axis.\n\nThe strangest things occur for simple linear transformations:\n\n```\nfig = plt.figure()\nax = SubplotHost(fig, 1,1,1)\nfig.add_subplot(ax)\n\nax2 = transformed_x_axis(ax, lambda x: 2. * x)\n\nx = np.linspace(1., 2., 200)\nax.plot(x, np.sin(x))\nplt.show()\n```\n\nHere, in the terminal, I see, again, no secondary axis. But in the notebook, I think one can see that something goes dramatically wrong:\n\n\nI think, it is the ticker that does those weird things when it faces transformed axes.\n\nAnyway, the behavior is inconsistent, unexpected and very weird.\n\nCheers, Gerhard\n\n---\n\nPython 2.7.12, matplotlib 1.5.1 on Windows 64 bit\neverything installed via Anaconda\n","author":{"url":"https://github.com/griai","@type":"Person","name":"griai"},"datePublished":"2016-10-12T12:17:41.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/7258/matplotlib/issues/7258"}
| 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:a673059a-2c3c-55af-3480-859f16679610 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A7C:2A1FE2:A2EA8E:DB4B31:6A53140E |
| html-safe-nonce | eae335c556f1b284946fcaf4e84040ecbec7229d581785d210d89eceea598c27 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTdDOjJBMUZFMjpBMkVBOEU6REI0QjMxOjZBNTMxNDBFIiwidmlzaXRvcl9pZCI6IjE1MzY5NzgzODM5NDIxMjg2NTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c022bd87b4d179da4f22b0843f52bc240e4f7216209d6340572ef6134a4d16d8 |
| hovercard-subject-tag | issue:182510537 |
| 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/7258/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d086212254fb7fb8902bd7ab38bbebabec7cdded0511c60efc8c6588be2e12b4/matplotlib/matplotlib/issues/7258 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d086212254fb7fb8902bd7ab38bbebabec7cdded0511c60efc8c6588be2e12b4/matplotlib/matplotlib/issues/7258 |
| og:image:alt | For several plots I'd like to have a secondary x axis that is a non-affine transformation of the original axis. Following a very old discussion on nabble I recognized that it is only possible, if I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | griai |
| 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