Title: Issue · GitHub
Open Graph Title: Issue · matplotlib/matplotlib
X Title: Issue · matplotlib/matplotlib
Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
Open Graph Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
X Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
Opengraph URL: https://github.com/matplotlib/matplotlib
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"canvas.draw() does not render tick labels with pdf backend","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n\r\nMy issue is closely related to the [one here](https://github.com/matplotlib/matplotlib/issues/6103).\r\nI need to access the ticklabels before writing my plot to a file, but when being non interactive there appears to be no way of forcing the generation of ticklabels, or is there? Both `fig.canvas.draw()` and `matplotlib.pyplot.draw()` have no effect.\r\n\r\nOf course I could generate my own ticklabels, using `ax.set_xticklabels(ax.get_xticks())` but I very much like the default formatting of ticks so I would like to rely on matplotlib here. \r\n\r\nIn the code below method 0 is what I would label the clean solution whereas all other implementations are workarounds, necessary to compensate for the difference of the behaviors of interactive vs non interactive backends. I find the dependence on the interactiveness of the backend problematic. This is because what I often do is to develop a plotting code using an interactive backend and when it's ready I apply it to big data on a HPC were I'm not always provided interactive backends. \r\n\r\n**Code for reproduction**\r\n\r\n```python\r\n#!/usr/bin/env python3\r\n\r\nimport matplotlib\r\nmatplotlib.use('pdf')\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.ticker as ticker\r\nimport numpy as np\r\n\r\nprint('backend:',matplotlib.get_backend())\r\n\r\nx = np.linspace(0,3,100)\r\ny = np.sin(x)**2\r\n\r\nmethod = 0\r\nwhile True:\r\n fig, ax = plt.subplots()\r\n ax.plot(x,y)\r\n if method == 0:\r\n fig.canvas.draw()\r\n plt.draw()\r\n elif method == 1:\r\n ax.xaxis.set_major_locator(ticker.AutoLocator())\r\n ax.xaxis.set_major_formatter(ticker.ScalarFormatter())\r\n elif method == 2:\r\n ax.set_xticklabels(ax.get_xticks())\r\n elif method == 3:\r\n ax.set_xticklabels([ ticker.ScalarFormatter().format_data(tick) for tick in ax.get_xticks() ])\r\n elif method == 4:\r\n ax.set_xticklabels([ '{:g}'.format(tick) for tick in ax.get_xticks() ])\r\n else:\r\n break\r\n \r\n print('method {:d} :'.format(method), end='')\r\n print([ label.get_text() for label in ax.get_xticklabels() ] )\r\n method += 1\r\n```\r\n\r\n**Actual outcome**\r\n\r\n```\r\nbackend: pdf\r\nmethod 0 :['', '', '', '', '', '', '', '', '']\r\nmethod 1 :['', '', '', '', '', '', '', '', '']\r\nmethod 2 :['-0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']\r\nmethod 3 :['−5e−1', '0', '5e−1', '1', '1.5', '2', '2.5', '3', '3.5']\r\nmethod 4 :['-0.5', '0', '0.5', '1', '1.5', '2', '2.5', '3', '3.5']\r\n```\r\n\r\n**Expected outcome**\r\n\r\n```\r\nbackend: pdf\r\nmethod 0 :['−0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']\r\nmethod 1 :['', '', '', '', '', '', '', '', '']\r\nmethod 2 :['-0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']\r\nmethod 3 :['−5e−1', '0', '5e−1', '1', '1.5', '2', '2.5', '3', '3.5']\r\nmethod 4 :['-0.5', '0', '0.5', '1', '1.5', '2', '2.5', '3', '3.5']\r\n```\r\nWhich (apart from the first line of course) is obtained when using an interactive backend.\r\n\r\n**Matplotlib version**\r\n * Operating system: Ubuntu 16.04.5 LTS 64-bit\r\n * Matplotlib version: 3.0.2 (conda, build: py36_1002, channel: conda-forge)\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): pdf\r\n * Python version: 3.6.8 (conda, build: h0371630_0)\r\n\r\n","author":{"url":"https://github.com/baloe","@type":"Person","name":"baloe"},"datePublished":"2019-02-25T12:14:51.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/13513/matplotlib/issues/13513"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:ac67e30b-0017-7649-e5ba-3064be633e8f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB42:272982:5200B7:6AFDC5:6A52F9E8 |
| html-safe-nonce | 04311f96435b1fbe6a2167503037095aeb76a72041f3d34f9867d97c5944975c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjQyOjI3Mjk4Mjo1MjAwQjc6NkFGREM1OjZBNTJGOUU4IiwidmlzaXRvcl9pZCI6IjMzMTQ2MzczNTMxNTk2ODY2MzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c6e1255428353b919a61966cfb67d1a6fd0fc6e94040e826991d688b42338c11 |
| hovercard-subject-tag | repository:1385122 |
| 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/matplotlib/matplotlib/issues/13513 |
| twitter:image | https://opengraph.githubassets.com/c30144758eec9e3324965530d5a5d5f25f1694c957399cc9975eb81431640b5e/matplotlib/matplotlib |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c30144758eec9e3324965530d5a5d5f25f1694c957399cc9975eb81431640b5e/matplotlib/matplotlib |
| og:image:alt | matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-cache |
| 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