Title: [Wishlist/candidate for gallery?] Diverging colormap taking into account asymmetry of the data about vcenter · Issue #12449 · matplotlib/matplotlib · GitHub
Open Graph Title: [Wishlist/candidate for gallery?] Diverging colormap taking into account asymmetry of the data about vcenter · Issue #12449 · matplotlib/matplotlib
X Title: [Wishlist/candidate for gallery?] Diverging colormap taking into account asymmetry of the data about vcenter · Issue #12449 · matplotlib/matplotlib
Description: I have been using a different approach to achieve what #12419 tries to address (DivergingNorm). The limitation of using something like DivergingNorm is that it doesn't allow for using color range proportional to the extent of the vmax, v...
Open Graph Description: I have been using a different approach to achieve what #12419 tries to address (DivergingNorm). The limitation of using something like DivergingNorm is that it doesn't allow for using color range p...
X Description: I have been using a different approach to achieve what #12419 tries to address (DivergingNorm). The limitation of using something like DivergingNorm is that it doesn't allow for using color ran...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/12449
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Wishlist/candidate for gallery?] Diverging colormap taking into account asymmetry of the data about vcenter","articleBody":"I have been using a different approach to achieve what #12419 tries to address (`DivergingNorm`).\r\nThe limitation of using something like `DivergingNorm` is that it doesn't allow for using color range proportional to the extent of the vmax, vmin w.r.t. `vcenter` (please see the example below). I have been using the following hackish approach to get over this limitation. This approach also allows for using colorbar extensions with the ability to specify different colors for `data \u003e vmax` and/or `data \u003c vmin` [based on my understanding, this won't be possible if `DivergingNorm` is used]. \r\n\r\nWould this be a useful addition to the gallery? Does it look like a functionality that I should think of adding to Matplotlib? (Also, please let me know if there is an easier way of achieving the same result).\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.colors as mcolors\r\nimport copy\r\n\r\ndef div_proportional_colormap(cmap='coolwarm', vmin=-1., vmax=1.,\r\n vcenter=0., cmap_name='my_cmap'):\r\n \"\"\" Given a diverging colormap, this returns a diverging\r\n colormap with color ranges on either side proportional to\r\n the extent of vmax, vmin w.r.t. vcenter i.e. vmax-vcenter, vcenter-vmin\r\n \"\"\"\r\n if isinstance(cmap, str):\r\n\t cmap = cm.get_cmap(cmap)\r\n\r\n max_delta_v= max(vcenter-vmin, vmax-vcenter)\r\n cmap_min = 0.5-0.5*(vcenter-vmin)/max_delta_v\r\n cmap_max = 0.5+0.5*(vmax-vcenter)/max_delta_v\r\n my_colors = cmap(np.linspace(cmap_min, cmap_max, 512))\r\n my_cmap = mcolors.LinearSegmentedColormap.from_list(cmap_name, my_colors)\r\n return my_cmap\r\n\r\nx = np.linspace(-2, 7)\r\ny = np.linspace(-1*np.pi, np.pi)\r\nX, Y = np.meshgrid(x, y)\r\nZ = x * np.sin(Y)**2\r\n\r\nfig, (ax1, ax2) = plt.subplots(ncols=2)\r\nmy_cmap1 = div_proportional_colormap(cmap=plt.cm.coolwarm,\r\n vmin=-2., vmax=6., vcenter=0.,\r\n cmap_name='my_cmap1')\r\nmy_cmap2 = copy.copy(my_cmap1)\r\nmy_cmap2.set_over('orange')\r\n\r\nimg1 = ax1.pcolormesh(Z, cmap=my_cmap1, vmin=-2, vmax=6)\r\ncbar1 = fig.colorbar(img1, ax=ax1)\r\n\r\nimg2 = ax2.pcolormesh(Z, cmap=my_cmap2, vmin=-2, vmax=6)\r\ncbar2 = fig.colorbar(img2, ax=ax2, extend='max')\r\nplt.show()\r\n```\r\n\r\n","author":{"url":"https://github.com/pharshalp","@type":"Person","name":"pharshalp"},"datePublished":"2018-10-08T19:12:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/12449/matplotlib/issues/12449"}
| 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:cea425fd-bca4-abd9-3e14-550c47f4ba4f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E560:17598F:40C7A8D:5A0C2EF:6A54D9C9 |
| html-safe-nonce | 66acdf1b6b330e98cf6bff4fdaec21d7e0111af262afcaf7a656cd89466a3a22 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTYwOjE3NTk4Rjo0MEM3QThEOjVBMEMyRUY6NkE1NEQ5QzkiLCJ2aXNpdG9yX2lkIjoiNDAzNzMwMjMzOTg2MjA1OTQ2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 27c95010cff208cf5b78c7ecdaf8916a03ad8664283373e1818bcda70fff0f4c |
| hovercard-subject-tag | issue:367917909 |
| 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/12449/issue_layout |
| twitter:image | https://opengraph.githubassets.com/60cd7d652fcdc9c923ce992b9b01fcd06036bc02be792463e10ca1c09bd4a5b0/matplotlib/matplotlib/issues/12449 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/60cd7d652fcdc9c923ce992b9b01fcd06036bc02be792463e10ca1c09bd4a5b0/matplotlib/matplotlib/issues/12449 |
| og:image:alt | I have been using a different approach to achieve what #12419 tries to address (DivergingNorm). The limitation of using something like DivergingNorm is that it doesn't allow for using color range p... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pharshalp |
| hostname | github.com |
| expected-hostname | github.com |
| None | a556215b071af6609619e2bbe6f00bdfbf0812ad723b1ae6c301858a7a829f54 |
| 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 | 00f21e38abd06cd6752db5c227083570f72fdb6c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width