FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

canvas.draw() does not render tick labels with pdf backend · Issue #13513 · matplotlib/matplotlib · GitHub

Repository navigation

canvas.draw() does not render tick labels with pdf backend #13513

Closed

Description

Bug report

Bug summary

My issue is closely related to the one here.
I 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.

Of 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.

In 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.

Code for reproduction

#!/usr/bin/env python3

import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

print('backend:',matplotlib.get_backend())

x = np.linspace(0,3,100)
y = np.sin(x)**2

method = 0
while True:
    fig, ax = plt.subplots()
    ax.plot(x,y)
    if method == 0:
        fig.canvas.draw()
        plt.draw()
    elif method == 1:
        ax.xaxis.set_major_locator(ticker.AutoLocator())
        ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
    elif method == 2:
        ax.set_xticklabels(ax.get_xticks())
    elif method == 3:
        ax.set_xticklabels([ ticker.ScalarFormatter().format_data(tick) for tick in ax.get_xticks() ])
    elif method == 4:
        ax.set_xticklabels([ '{:g}'.format(tick) for tick in ax.get_xticks() ])
    else:
        break
    
    print('method {:d} :'.format(method), end='')
    print([ label.get_text() for label in ax.get_xticklabels() ] )
    method += 1

Actual outcome

backend: pdf
method 0 :['', '', '', '', '', '', '', '', '']
method 1 :['', '', '', '', '', '', '', '', '']
method 2 :['-0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']
method 3 :['−5e−1', '0', '5e−1', '1', '1.5', '2', '2.5', '3', '3.5']
method 4 :['-0.5', '0', '0.5', '1', '1.5', '2', '2.5', '3', '3.5']

Expected outcome

backend: pdf
method 0 :['−0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']
method 1 :['', '', '', '', '', '', '', '', '']
method 2 :['-0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']
method 3 :['−5e−1', '0', '5e−1', '1', '1.5', '2', '2.5', '3', '3.5']
method 4 :['-0.5', '0', '0.5', '1', '1.5', '2', '2.5', '3', '3.5']

Which (apart from the first line of course) is obtained when using an interactive backend.

Matplotlib version

  • Operating system: Ubuntu 16.04.5 LTS 64-bit
  • Matplotlib version: 3.0.2 (conda, build: py36_1002, channel: conda-forge)
  • Matplotlib backend (print(matplotlib.get_backend())): pdf
  • Python version: 3.6.8 (conda, build: h0371630_0)

Activity

  1. tacaswell commented on Feb 25, 2019

    Member

    if you change ot matplotlib.use('agg') does it work?

    Why do you need access to the tick labels?

  2. baloe commented on Feb 25, 2019

    Author

    agg, works fine, I guess I will just use aggs then.
    I'm writing a function that will take a figure, replace all pairs of $ ... $ by \$ ... \$ to turn off mathtex rendering, and then save it to an svg file. I then, using inkscape, convert this svg file into a pdf+pdf_tex file pair for inclusion in latex documents.
    I'm not a big fan of pgf but I adore inkscape's way of splitting all graphics from latex by writing the graphics to the pdf document and the latex to the pdf_tex document.

    This is indeed pretty much a workaround the issue that one cannot turn off mathtex rendering in matplotlib, but while I'm at it, it also allows me to have ticklabels formatted as $\ticklabel{0.1}$, $\ticklabel{0.2}$ ... so that I can easily modify the appearance of ticks in my latex document later on.

  3. tacaswell commented on Feb 25, 2019

    Member

    You may be interested in the discussion around #12856

    I think you have identified an app specific behavior we (at least I) have been thinking of as 'standard' behavior.

  4. changed the title [-]Force generation of ticklabels when non interactive[/-] [+]canvas.draw() does not render tick labels with pdf backend[/+] on Feb 25, 2019
  5. jklymak commented on Mar 3, 2019

    Member

    How are you saving the file? Is there any reason to use the pdf backend except for saving? Not saying this isn't a bug, but its a bit funny to use pdf as a non-printing backend; i.e. matplotlib.use('egg') with fig.sqvefig('boo.pdf') works?

  6. github-actions commented on Jun 1, 2023

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  7. jklymak commented on Jun 1, 2023

    Member

    I'll close this as the vector backends are not particularly meant to be used "live" like this and really should only be used for saving finished figures.

  8. added this to the v3.4.0 milestone on Jun 1, 2023
  9. tacaswell commented on Jun 1, 2023

    Member

    The core of the problem as that we are lazy about generating tick labels, but the user wants to access them to change the strings.

    I think what the OP wants here is the fig.draw_without_rendering() method, but
    this was actually fixed by #18408 in mpl 3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL