Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: tight_layout (version 3.5+) #22673

Closed
2sn opened this issue Mar 20, 2022 · 76 comments · Fixed by #22732
Closed

[Bug]: tight_layout (version 3.5+) #22673

2sn opened this issue Mar 20, 2022 · 76 comments · Fixed by #22732
Labels
status: needs clarification Issues that need more information to resolve.
Milestone

Comments

@2sn
Copy link

2sn commented Mar 20, 2022

Bug summary

fig.tight_layout is broken in current matplotlib.

Code for reproduction

%pylab
plot([0,1])
tight_layout()

# happens same way in object mode
from matplotlib import pylab as plt
fig, ax = plt.subplots()
ax.plot([0,1])
fig.tight_layout()

Actual outcome

xxx

below another example from real code that may be instructive

xxx

Expected outcome

figure scaled to image size, background deleted.

Additional information

Happens always.

I do not recall this issue in version before 3.5

I have no clue why it is happening. The flaw seems to lay in mpl getting/setting wrong window size when using tight_layout. Maybe gtk unhappy with X11. When I use mpl.use('Qt5Cairo') then it just makes the window and fonts smaller. Obviously, tight_layout should not change physical window size on screen! Similar for Gtk4Cairo. Gtk4Agg has similar issues to Qt5Agg but does not retain noise in background, just shrinks figure. I could be related to DPI settings. It might get them from a different system call than when opening the window?

The only fix I can find is use other plot package, e.g., plotly.

Operating system

Linux 5.16.14-200.fc35.x86_64 #1 SMP PREEMPT Fri Mar 11 20:31:18 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Matplotlib Version

3.5.1

Matplotlib Backend

Qt5Agg

Python version

Python 3.10.2

Jupyter version

N/A

Installation

pip

@2sn 2sn changed the title [Bug]: tight_layout (v [Bug]: tight_layout (version 3.5+) Mar 20, 2022
@2sn
Copy link
Author

2sn commented Mar 20, 2022

TkAgg and WxAgg give same (broken) results.

@jklymak
Copy link
Member

jklymak commented Mar 20, 2022

Are you using a window tiling manager? Perhaps it is doing weird things with the window dpi. Tight layout doesn't change window size but it does do multiple draws. I suspect somehow that between the draws your window manager is telling matplotlib that the dpi or dpi ratio has changed.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

No, just regular windows.
I have never seen it before mpl Version 3.4.
It always changes from original window when I call tinge_layout the first time in a window.
it theon only plots in the smaller fraction of the window, or the shrunk window.
when I resize window thereafter, it fills the window, but retains the smaller font.
So it seems tight_layout gets dpi from somewhere else - wrongly and new to mpl 3.5+ - from the initial call, and then sets it permanently.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

specifically, it sets dpi to 72 whereas the original value is 102.
72 seems to be default value, so something is broken and ut uses default over actual value.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

In [13]: plot([0,1])
Out[13]: [<matplotlib.lines.Line2D at 0x7f3a4c9d5c60>]

In [14]: gcf().dpi
Out[14]: 100.0

In [15]: tight_layout()

In [16]: gcf().dpi
Out[16]: 72

@2sn
Copy link
Author

2sn commented Mar 20, 2022

Another interesting example

Python 3.10.2 (main, Jan 28 2022, 00:32:56) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: Qt5Agg
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib

In [1]: plot([0,1])
Out[1]: [<matplotlib.lines.Line2D at 0x7ff89f71a2c0>]

In [2]: from matplotlib.backend_bases import _get_renderer

In [3]: renderer = _get_renderer(gcf())

In [4]: renderer.dpi
Out[4]: 100.0

In [5]: tight_layout()

In [6]: gcf().dpi
Out[6]: 72

In [7]: renderer.dpi
Out[7]: 100.0

Oops.

@QuLogic
Copy link
Member

QuLogic commented Mar 24, 2022

I cannot reproduce either of these effects.

@QuLogic
Copy link
Member

QuLogic commented Mar 24, 2022

%pylab is deprecated, use %matplotlib inline and import the required libraries.

Also, you probably shouldn't use this any more.

@2sn
Copy link
Author

2sn commented Mar 24, 2022

I am using IPython on the command line, not jupyter.

What was/is wrong with %pylab?
It seemed to have worked find and still does.

@2sn
Copy link
Author

2sn commented Mar 24, 2022

@QuLogic

%matplotlib inline

does not work.

In [1]: %matplotlib inline

In [2]: plot()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 plot()

@timhoffm
Copy link
Member

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot(...)

@jklymak
Copy link
Member

jklymak commented Mar 24, 2022

Something is changing your dpi_ratio between the draw triggered by plot and the draw triggered by tight_layout. I would be very surprised if this had anything to do with tight_layout per-se. Rather I think that the first call doesn't really know the dpi or dpi_ratio until the first window is made, and then it gets updated, and the subsequent window is resized.

If I use plt.ion(), I cannot reproduce your problem on MacOS.

from matplotlib import pylab as plt

try

import matplotlib.pyplot as plt

@jklymak
Copy link
Member

jklymak commented Mar 24, 2022

Other debugging steps:

  • let us know your window manager and any settings you have for it, including your dpi_ratio

  • make sure you do not have any settings in your matplotlibrc (move it somewhere else for now).

  • test as a script without interactive mode on.

    import matplotlib.pyplot as plt
    plt.plot([0, 1])
    plt.tight_layout()
    plt.show()

@2sn
Copy link
Author

2sn commented Mar 24, 2022

I think you first analysis is not correct. The resolution of 100 dpi it uses first is the correct one. The 72 dpi is a default value it must be getting elsewhere. From xdpyinfo:

screen #0:
  dimensions:    3840x2160 pixels (1016x571 millimeters)
  resolution:    96x96 dots per inch

@2sn
Copy link
Author

2sn commented Mar 24, 2022

window manager is fvwm2. But there was never an issue until the current mpl version ...

@2sn
Copy link
Author

2sn commented Mar 24, 2022

I follow you steps above and window is not rescaled. No issues.

@2sn
Copy link
Author

2sn commented Mar 24, 2022

it I enable my matplotlibrc file, the problem appears.
interactive mode in IPython is not an issue (no problem occurs).

The file, admittedly is dated ...

matplotlibrc

### MATPLOTLIBRC FORMAT

# This is a sample matplotlib configuration file - you can find a copy
# of it on your system in
# site-packages/matplotlib/mpl-data/matplotlibrc.  If you edit it
# there, please note that it will be overwritten in your next install.
# If you want to keep a permanent local copy that will not be
# overwritten, place it in the following location:
# unix/linux:
#     $HOME/.config/matplotlib/matplotlibrc or
#     $XDG_CONFIG_HOME/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME is set)
# other platforms:
#     $HOME/.matplotlib/matplotlibrc
#
# See http://matplotlib.org/users/customizing.html#the-matplotlibrc-file for
# more details on the paths which are checked for the configuration file.
#
# This file is best viewed in a editor which supports python mode
# syntax highlighting. Blank lines, or lines starting with a comment
# symbol, are ignored, as are trailing comments.  Other lines must
# have the format
#    key : val # optional comment
#
# Colors: for the color values below, you can either use - a
# matplotlib color string, such as r, k, or b - an rgb tuple, such as
# (1.0, 0.5, 0.0) - a hex string, such as ff00ff or #ff00ff - a scalar
# grayscale intensity such as 0.75 - a legal html color name, e.g., red,
# blue, darkslategray

#### CONFIGURATION BEGINS HERE

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
backend      : Qt5Agg
# backend      : gtk3cairo
# backend      : gtk3agg
# backend      : Tkagg
# backend : module://mplcairo.tk

# If you are using the Qt4Agg backend, you can choose here
# to use the PyQt4 bindings or the newer PySide bindings to
# the underlying Qt4 toolkit.
#backend.qt4 : PyQt4        # PyQt4 | PySide

# Note that this can be overridden by the environment variable
# QT_API used by Enthought Tool Suite (ETS); valid values are
# "pyqt" and "pyside".  The "pyqt" setting has the side effect of
# forcing the use of Version 2 API for QString and QVariant.

# The port to use for the web server in the WebAgg backend.
# webagg.port : 8888

# If webagg.port is unavailable, a number of other random ports will
# be tried until one that is available is found.
# webagg.port_retries : 50

# When True, open the webbrowser to the plot that is shown
# webagg.open_in_browser : True

# When True, the figures rendered in the nbagg backend are created with
# a transparent background.
# nbagg.transparent : True

# if you are running pyplot inside a GUI and your backend choice
# conflicts, we will automatically try to find a compatible one for
# you if backend_fallback is True
#backend_fallback: True

#interactive  : False
#toolbar      : toolbar2   # None | toolbar2  ("classic" is deprecated)
#timezone     : UTC        # a pytz timezone string, e.g., US/Central or Europe/Paris

# Where your matplotlib data lives if you installed to a non-default
# location.  This is where the matplotlib fonts, bitmaps, etc reside
#datapath : /home/jdhunter/mpldata


### LINES
# See http://matplotlib.org/api/artist_api.html#module-matplotlib.lines for more
# information on line properties.
#lines.linewidth   : 1.0     # line width in points
#lines.linestyle   : -       # solid line
#lines.color       : blue    # has no affect on plot(); see axes.color_cycle
#lines.marker      : None    # the default marker
#lines.markeredgewidth  : 0.5     # the line width around the marker symbol
#lines.markersize  : 6            # markersize, in points
#lines.dash_joinstyle : miter        # miter|round|bevel
#lines.dash_capstyle : butt          # butt|round|projecting
#lines.solid_joinstyle : miter       # miter|round|bevel
#lines.solid_capstyle : projecting   # butt|round|projecting
#lines.antialiased : True         # render lines in antialised (no jaggies)

#markers.fillstyle: full # full|left|right|bottom|top|none

### PATCHES
# Patches are graphical objects that fill 2D space, like polygons or
# circles.  See
# http://matplotlib.org/api/artist_api.html#module-matplotlib.patches
# information on patch properties
#patch.linewidth        : 1.0     # edge width in points
#patch.facecolor        : blue
#patch.edgecolor        : black
#patch.antialiased      : True    # render patches in antialised (no jaggies)

### FONT
#
# font properties used by text.Text.  See
# http://matplotlib.org/api/font_manager_api.html for more
# information on font properties.  The 6 font properties used for font
# matching are given below with their default values.
#
# The font.family property has five values: 'serif' (e.g., Times),
# 'sans-serif' (e.g., Helvetica), 'cursive' (e.g., Zapf-Chancery),
# 'fantasy' (e.g., Western), and 'monospace' (e.g., Courier).  Each of
# these font families has a default list of font names in decreasing
# order of priority associated with them.  When text.usetex is False,
# font.family may also be one or more concrete font names.
#
# The font.style property has three values: normal (or roman), italic
# or oblique.  The oblique style will be used for italic, if it is not
# present.
#
# The font.variant property has two values: normal or small-caps.  For
# TrueType fonts, which are scalable fonts, small-caps is equivalent
# to using a font size of 'smaller', or about 83%% of the current font
# size.
#
# The font.weight property has effectively 13 values: normal, bold,
# bolder, lighter, 100, 200, 300, ..., 900.  Normal is the same as
# 400, and bold is 700.  bolder and lighter are relative values with
# respect to the current weight.
#
# The font.stretch property has 11 values: ultra-condensed,
# extra-condensed, condensed, semi-condensed, normal, semi-expanded,
# expanded, extra-expanded, ultra-expanded, wider, and narrower.  This
# property is not currently implemented.
#
# The font.size property is the default font size for text, given in pts.
# 12pt is the standard value.
#
#font.family         : sans-serif
#font.style          : normal
#font.variant        : normal
#font.weight         : medium
#font.stretch        : normal
# note that font.size controls default text sizes.  To configure
# special text sizes tick labels, axes, labels, title, etc, see the rc
# settings for axes and ticks. Special text sizes can be defined
# relative to font.size, using the following values: xx-small, x-small,
# small, medium, large, x-large, xx-large, larger, or smaller
#font.size           : 12.0
#font.serif          : Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
#font.sans-serif     : Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#font.cursive        : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
#font.fantasy        : Comic Sans MS, Chicago, Charcoal, Impact, Western, Humor Sans, fantasy
#font.monospace      : Bitstream Vera Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

### TEXT
# text properties used by text.Text.  See
# http://matplotlib.org/api/artist_api.html#module-matplotlib.text for more
# information on text properties

#text.color          : black

### LaTeX customizations. See http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex
#text.usetex         : False  # use latex for all text handling. The following fonts
                              # are supported through the usual rc parameter settings:
                              # new century schoolbook, bookman, times, palatino,
                              # zapf chancery, charter, serif, sans-serif, helvetica,
                              # avant garde, courier, monospace, computer modern roman,
                              # computer modern sans serif, computer modern typewriter
                              # If another font is desired which can loaded using the
                              # LaTeX \usepackage command, please inquire at the
                              # matplotlib mailing list
#text.latex.unicode : False # use "ucs" and "inputenc" LaTeX packages for handling
                            # unicode strings.
#text.latex.preamble :  # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
                            # AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
                            # IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
                            # preamble is a comma separated list of LaTeX statements
                            # that are included in the LaTeX document preamble.
                            # An example:
                            # text.latex.preamble : \usepackage{bm},\usepackage{euler}
                            # The following packages are always loaded with usetex, so
                            # beware of package collisions: color, geometry, graphicx,
                            # type1cm, textcomp. Adobe Postscript (PSSNFS) font packages
                            # may also be loaded, depending on your font settings

#text.dvipnghack : None      # some versions of dvipng don't handle alpha
                             # channel properly.  Use True to correct
                             # and flush ~/.matplotlib/tex.cache
                             # before testing and False to force
                             # correction off.  None will try and
                             # guess based on your dvipng version

#text.hinting : auto   # May be one of the following:
                       #   'none': Perform no hinting
                       #   'auto': Use freetype's autohinter
                       #   'native': Use the hinting information in the
                       #             font file, if available, and if your
                       #             freetype library supports it
                       #   'either': Use the native hinting information,
                       #             or the autohinter if none is available.
                       # For backward compatibility, this value may also be
                       # True === 'auto' or False === 'none'.
#text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
                         # horizontal direction.  A value of 1 will hint to full
                         # pixels.  A value of 2 will hint to half pixels etc.

#text.antialiased : True # If True (default), the text will be antialiased.
                         # This only affects the Agg backend.

# The following settings allow you to select the fonts in math mode.
# They map from a TeX font name to a fontconfig font pattern.
# These settings are only used if mathtext.fontset is 'custom'.
# Note that this "custom" mode is unsupported and may go away in the
# future.
#mathtext.cal : cursive
#mathtext.rm  : serif
#mathtext.tt  : monospace
#mathtext.it  : serif:italic
#mathtext.bf  : serif:bold
#mathtext.sf  : sans
#mathtext.fontset : cm # Should be 'cm' (Computer Modern), 'stix',
                       # 'stixsans' or 'custom'
#mathtext.fallback_to_cm : True  # When True, use symbols from the Computer Modern
                                 # fonts when a symbol can not be found in one of
                                 # the custom math fonts.

#mathtext.default : it # The default font to use for math.
                       # Can be any of the LaTeX font names, including
                       # the special name "regular" for the same font
                       # used in regular text.

### AXES
# default face and edge color, default tick sizes,
# default fontsizes for ticklabels, and so on.  See
# http://matplotlib.org/api/axes_api.html#module-matplotlib.axes
#axes.hold           : True    # whether to clear the axes by default on
#axes.facecolor      : white   # axes background color
#axes.edgecolor      : black   # axes edge color
#axes.linewidth      : 1.0     # edge linewidth
#axes.grid           : False   # display grid or not
#axes.titlesize      : large   # fontsize of the axes title
#axes.labelsize      : medium  # fontsize of the x any y labels
#axes.labelpad       : 5.0     # space between label and axis
#axes.labelweight    : normal  # weight of the x and y labels
#axes.labelcolor     : black
#axes.axisbelow      : False   # whether axis gridlines and ticks are below
                               # the axes elements (lines, text, etc)

#axes.formatter.limits : -7, 7 # use scientific notation if log10
                               # of the axis range is smaller than the
                               # first or larger than the second
#axes.formatter.use_locale : False # When True, format tick labels
                                   # according to the user's locale.
                                   # For example, use ',' as a decimal
                                   # separator in the fr_FR locale.
#axes.formatter.use_mathtext : False # When True, use mathtext for scientific
                                     # notation.
#axes.formatter.useoffset      : True    # If True, the tick label formatter
                                         # will default to labeling ticks relative
                                         # to an offset when the data range is very
                                         # small compared to the minimum absolute
                                         # value of the data.

#axes.unicode_minus  : True    # use unicode for the minus symbol
                               # rather than hyphen.  See
                               # http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
#axes.color_cycle    : b, g, r, c, m, y, k  # color cycle for plot lines
                                            # as list of string colorspecs:
                                            # single letter, long name, or
                                            # web-style hex
#axes.xmargin        : 0  # x margin.  See `axes.Axes.margins`
#axes.ymargin        : 0  # y margin See `axes.Axes.margins`

#polaraxes.grid      : True    # display grid on polar axes
#axes3d.grid         : True    # display grid on 3d axes

### TICKS
# see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick
#xtick.major.size     : 4      # major tick size in points
#xtick.minor.size     : 2      # minor tick size in points
#xtick.major.width    : 0.5    # major tick width in points
#xtick.minor.width    : 0.5    # minor tick width in points
#xtick.major.pad      : 4      # distance to major tick label in points
#xtick.minor.pad      : 4      # distance to the minor tick label in points
#xtick.color          : k      # color of the tick labels
#xtick.labelsize      : medium # fontsize of the tick labels
#xtick.direction      : in     # direction: in, out, or inout

#ytick.major.size     : 4      # major tick size in points
#ytick.minor.size     : 2      # minor tick size in points
#ytick.major.width    : 0.5    # major tick width in points
#ytick.minor.width    : 0.5    # minor tick width in points
#ytick.major.pad      : 4      # distance to major tick label in points
#ytick.minor.pad      : 4      # distance to the minor tick label in points
#ytick.color          : k      # color of the tick labels
#ytick.labelsize      : medium # fontsize of the tick labels
#ytick.direction      : in     # direction: in, out, or inout


### GRIDS
#grid.color       :   black   # grid color
#grid.linestyle   :   :       # dotted
#grid.linewidth   :   0.5     # in points
#grid.alpha       :   1.0     # transparency, between 0.0 and 1.0

### Legend
#legend.fancybox      : False  # if True, use a rounded box for the
                               # legend, else a rectangle
#legend.isaxes        : True
#legend.numpoints     : 2      # the number of points in the legend line
#legend.fontsize      : large
#legend.borderpad     : 0.5    # border whitespace in fontsize units
#legend.markerscale   : 1.0    # the relative size of legend markers vs. original
# the following dimensions are in axes coords
#legend.labelspacing  : 0.5    # the vertical space between the legend entries in fraction of fontsize
#legend.handlelength  : 2.     # the length of the legend lines in fraction of fontsize
#legend.handleheight  : 0.7     # the height of the legend handle in fraction of fontsize
#legend.handletextpad : 0.8    # the space between the legend line and legend text in fraction of fontsize
#legend.borderaxespad : 0.5   # the border between the axes and legend edge in fraction of fontsize
#legend.columnspacing : 2.    # the border between the axes and legend edge in fraction of fontsize
#legend.shadow        : False
#legend.frameon       : True   # whether or not to draw a frame around legend
#legend.framealpha    : None    # opacity of of legend frame
#legend.scatterpoints : 3 # number of scatter points

### FIGURE
# See http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure
#figure.titlesize : medium     # size of the figure title
#figure.titleweight : normal   # weight of the figure title
#figure.figsize   : 8, 6    # figure size in inches
#figure.dpi       : 102      # figure dots per inch
figure.facecolor : 1.0    # figure facecolor; 0.75 is scalar gray
#figure.edgecolor : white   # figure edgecolor
#figure.autolayout : False  # When True, automatically adjust subplot
                            # parameters to make the plot fit the figure
#figure.max_open_warning : 20  # The maximum number of figures to open through
                               # the pyplot interface before emitting a warning.
                               # If less than one this feature is disabled.

# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
#figure.subplot.left    : 0.125  # the left side of the subplots of the figure
#figure.subplot.right   : 0.9    # the right side of the subplots of the figure
#figure.subplot.bottom  : 0.1    # the bottom of the subplots of the figure
#figure.subplot.top     : 0.9    # the top of the subplots of the figure
#figure.subplot.wspace  : 0.2    # the amount of width reserved for blank space between subplots
#figure.subplot.hspace  : 0.2    # the amount of height reserved for white space between subplots

### IMAGES
#image.aspect : equal             # equal | auto | a number
#image.interpolation  : bilinear  # see help(imshow) for options
#image.cmap   : jet               # gray | jet etc...
#image.lut    : 256               # the size of the colormap lookup table
#image.origin : upper             # lower | upper
#image.resample  : False
#image.composite_image : True     # When True, all the images on a set of axes are
                                  # combined into a single composite image before
                                  # saving a figure as a vector graphics file,
                                  # such as a PDF.

### CONTOUR PLOTS
#contour.negative_linestyle : dashed # dashed | solid
#contour.corner_mask        : True   # True | False | legacy

### ERRORBAR PLOTS
#errorbar.capsize : 3             # length of end cap on error bars in pixels

### Agg rendering
### Warning: experimental, 2008/10/10
#agg.path.chunksize : 0           # 0 to disable; values in the range
                                  # 10000 to 100000 can improve speed slightly
                                  # and prevent an Agg rendering failure
                                  # when plotting very large data sets,
                                  # especially if they are very gappy.
                                  # It may cause minor artifacts, though.
                                  # A value of 20000 is probably a good
                                  # starting point.
### SAVING FIGURES
#path.simplify : True   # When True, simplify paths by removing "invisible"
                        # points to reduce file size and increase rendering
                        # speed
#path.simplify_threshold : 0.1  # The threshold of similarity below which
                                # vertices will be removed in the simplification
                                # process
#path.snap : True # When True, rectilinear axis-aligned paths will be snapped to
                  # the nearest pixel when certain criteria are met.  When False,
                  # paths will never be snapped.
#path.sketch : None # May be none, or a 3-tuple of the form (scale, length,
                    # randomness).
                    # *scale* is the amplitude of the wiggle
                    # perpendicular to the line (in pixels).  *length*
                    # is the length of the wiggle along the line (in
                    # pixels).  *randomness* is the factor by which
                    # the length is randomly scaled.

# the default savefig params can be different from the display params
# e.g., you may want a higher resolution, or to make the figure
# background white
#savefig.dpi         : 100      # figure dots per inch
#savefig.facecolor   : white    # figure facecolor when saving
#savefig.edgecolor   : white    # figure edgecolor when saving
savefig.format       : pdf      # png, ps, pdf, svg
#savefig.bbox        : standard # 'tight' or 'standard'.
                                # 'tight' is incompatible with pipe-based animation
                                # backends but will workd with temporary file based ones:
                                # e.g. setting animation.writer to ffmpeg will not work,
                                # use ffmpeg_file instead
#savefig.pad_inches  : 0.1      # Padding to be used when bbox is set to 'tight'
#savefig.jpeg_quality: 95       # when a jpeg is saved, the default quality parameter.
#savefig.directory   : ~        # default directory in savefig dialog box,
                                # leave empty to always use current working directory
#savefig.transparent : False    # setting that controls whether figures are saved with a
                                # transparent background by default

# tk backend params
#tk.window_focus   : False    # Maintain shell focus for TkAgg

# ps backend params
#ps.papersize      : letter   # auto, letter, legal, ledger, A0-A10, B0-B10
#ps.useafm         : False    # use of afm fonts, results in small files
#ps.usedistiller   : False    # can be: None, ghostscript or xpdf
                                          # Experimental: may produce smaller files.
                                          # xpdf intended for production of publication quality files,
                                          # but requires ghostscript, xpdf and ps2eps
#ps.distiller.res  : 6000      # dpi
#ps.fonttype       : 3         # Output Type 3 (Type3) or Type 42 (TrueType)

# pdf backend params
pdf.compression   : 9 # integer from 0 to 9
                       # 0 disables compression (good for debugging)
pdf.fonttype       : 42         # Output Type 3 (Type3) or Type 42 (TrueType)

# svg backend params
#svg.image_inline : True       # write raster image data directly into the svg file
#svg.image_noscale : False     # suppress scaling of raster data embedded in SVG
#svg.fonttype : 'path'         # How to handle SVG fonts:
#    'none': Assume fonts are installed on the machine where the SVG will be viewed.
#    'path': Embed characters as paths -- supported by most SVG renderers
#    'svgfont': Embed characters as SVG fonts -- supported only by Chrome,
#               Opera and Safari

# docstring params
#docstring.hardcopy = False  # set this when you want to generate hardcopy docstring

# Set the verbose flags.  This controls how much information
# matplotlib gives you at runtime and where it goes.  The verbosity
# levels are: silent, helpful, debug, debug-annoying.  Any level is
# inclusive of all the levels below it.  If your setting is "debug",
# you'll get all the debug and helpful messages.  When submitting
# problems to the mailing-list, please set verbose to "helpful" or "debug"
# and paste the output into your report.
#
# The "fileo" gives the destination for any calls to verbose.report.
# These objects can a filename, or a filehandle like sys.stdout.
#
# You can override the rc default verbosity from the command line by
# giving the flags --verbose-LEVEL where LEVEL is one of the legal
# levels, e.g., --verbose-helpful.
#
# You can access the verbose instance in your code
#   from matplotlib import verbose.
#verbose.level  : silent      # one of silent, helpful, debug, debug-annoying
#verbose.fileo  : sys.stdout  # a log filename, sys.stdout or sys.stderr

# Event keys to interact with figures/plots via keyboard.
# Customize these settings according to your needs.
# Leave the field(s) empty if you don't need a key-map. (i.e., fullscreen : '')

#keymap.fullscreen : f               # toggling
#keymap.home : h, r, home            # home or reset mnemonic
#keymap.back : left, c, backspace    # forward / backward keys to enable
#keymap.forward : right, v           #   left handed quick navigation
#keymap.pan : p                      # pan mnemonic
#keymap.zoom : o                     # zoom mnemonic
#keymap.save : s                     # saving current figure
#keymap.quit : ctrl+w, cmd+w         # close the current figure
#keymap.grid : g                     # switching on/off a grid in current axes
#keymap.yscale : l                   # toggle scaling of y-axes ('log'/'linear')
#keymap.xscale : L, k                # toggle scaling of x-axes ('log'/'linear')
#keymap.all_axes : a                 # enable all axes

# Control location of examples data files
#examples.directory : ''   # directory to look in for custom installation

###ANIMATION settings
#animation.html : 'none'           # How to display the animation as HTML in
                                   # the IPython notebook. 'html5' uses
                                   # HTML5 video tag.
#animation.writer : ffmpeg         # MovieWriter 'backend' to use
#animation.codec : mpeg4           # Codec to use for writing movie
#animation.bitrate: -1             # Controls size/quality tradeoff for movie.
                                   # -1 implies let utility auto-determine
#animation.frame_format: 'png'     # Controls frame format used by temp files
#animation.ffmpeg_path: 'ffmpeg'   # Path to ffmpeg binary. Without full path
                                   # $PATH is searched
#animation.ffmpeg_args: ''         # Additional arguments to pass to ffmpeg
#animation.avconv_path: 'avconv'   # Path to avconv binary. Without full path
                                   # $PATH is searched
#animation.avconv_args: ''         # Additional arguments to pass to avconv
#animation.mencoder_path: 'mencoder'
                                   # Path to mencoder binary. Without full path
                                   # $PATH is searched
#animation.mencoder_args: ''       # Additional arguments to pass to mencoder
#animation.convert_path: 'convert' # Path to ImageMagick's convert binary.
                                   # On Windows use the full path since convert

@jklymak
Copy link
Member

jklymak commented Mar 24, 2022

it I enable my matplotlibrc file, the problem appears.
interactive mode in IPython is not an issue (no problem occurs).

Can you clarify both these statements? I can't tell exactly what you did. Thanks.

@2sn
Copy link
Author

2sn commented Mar 24, 2022

I use a custom ipython config file that loads %pylab with interactive mode. Ding so does not break resolution.

If I use the above matplotlibrc file, the error occurs, independent whether %pylab is loaded or not.

So the bug seems to be triggered by my matplotlibrc file, which I have been using mostly unchanged since a decade.

@jklymak
Copy link
Member

jklymak commented Mar 25, 2022

Your matplotlibrc file only uses Qt5Agg, so far as I can see. The rest of it is commented out. If you don't use Qt5Agg, you are presumably using TkAgg, which you said above is also broken, but now you say it is working? Hard for us to track down, but my guess would be you have a broken Qt5 installation. Perhaps upgrade pyqt?

@MarkWieczorek
Copy link

I can confirm/reproduce this bug. tight_layout() seems to be doing something weird with the figure dpi. I note that I manually set the dpi to 114 to accommodate the screen that I work with:

In [1]: mpl.rcParams['figure.dpi'] = 114

In [2]: fig, ax = plt.subplots(1, 1)

In [3]: plt.gcf().dpi
Out[3]: 114.0

In [4]: fig.tight_layout()

In [5]: plt.gcf().dpi
Out[5]: 72

As a result of this, tight_layout() does not change the physical size of the figure on the screen, but it does reduce the size of the graphic that you plot in the figure.

macos (intel)
python: 3.9.12
matplotlib-base: 3.5.1

@jklymak
Copy link
Member

jklymak commented Mar 29, 2022

@MarkWieczorek Can you include your imports and the backend you are using? You must have something set in a configuration file somewhere to not need to execute the imports...

On MacOS, master, if I do

In [1]: import matplotlib as mpl

In [2]: mpl.rcParams['figure.dpi'] = 114

In [3]: import matplotlib.pyplot as plt

In [4]: fig, ax = plt.subplots(1, 1)

In [5]: plt.gcf().dpi
Out[5]: 228.0

In [6]: fig.tight_layout()

In [7]: plt.gcf().dpi
Out[7]: 228.0

In [8]: print(mpl.get_backend())
Out[8]: MacOSX

... you can complain that the dpi "changed" but that is because I am working on a Retina screen.

@MarkWieczorek
Copy link

After starting with a base environment with only matplotlib-base installed I note the following:

  • First, I have a retina screen but my screen dpi didn't get upgraded like yours did. I don't understand this.

  • Second, I can reproduce your results above. This suggests that one of the 50+ packages that I have installed could be causing this problem.

  • Third, as it will be extremely time consuming to isolate which package is causing this error, my approach is different: How is it that importing a package could cause fig.tight_layout() to work improperly ???

I note that I am using the default "MacOS" backend.

@jklymak
Copy link
Member

jklymak commented Mar 29, 2022

If a fresh install behaves properly, my guess would be you have a crossed install somehow. I would just rebuild my environment until it fails, perhaps starting with removing Matplotlib and re-installing it (and any dependencies ti takes with it). Don't remove the old environment, just make a fresh one.

@QuLogic
Copy link
Member

QuLogic commented Mar 30, 2022

On MacOS, master, if I do

Note that @greglucas has made some improvements to HiDPI handling on the macOS backend that are only in main.

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

My tests were in a fresh conda env on 3.5.1 using Matplotlib-base. However, its always possible I have a crossed install!

@2sn
Copy link
Author

2sn commented Mar 30, 2022

For me, it seems that just the presence of the customised matplotlibrc causes the issue.

Something must be called differently by tight_layout for the dpi. Why does tight_layout change dpi in the first place?

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

tight_layout does not (directly) change the dpi.

@2sn
Copy link
Author

2sn commented Mar 30, 2022

What, then causes the actual change? It must be calling some functions that implicitly reset it. I suspect it needs it for calculations, but when obtaining it the function used changes it where it should not?

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

I dunno, I am going to take a wild guess that someone monkey patches us in both your tool chains and breaks our renderer dance. It would be helpful to know which package, but unless we can find a Matplotlib-only reproducer, its hard of us to move forward.

@jklymak jklymak added the status: needs clarification Issues that need more information to resolve. label Mar 30, 2022
@2sn
Copy link
Author

2sn commented Mar 30, 2022

On MacOS (M1), I use homebrew with current python 3.10.2, current IPython (but irrelevant) installed from pip.
commands in IPython with pylab (but both are irrelevant) simpy are

plot([0,1])
tight_layout()

to get the misfit window/dpi patch. And it only depends on the

savefig.format: pdf

config line.

I do not use conda. But I do use Python 3.10.2.

I would think a homebrew install of python on MacOS is fairly standard.

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

commands in IPython with pylab (but both are irrelevant) simpy are

Why do you think they are irrelevant? Please test without ipython.

@2sn
Copy link
Author

2sn commented Mar 30, 2022

I have installed some 100 packages, I am happy to provide the list but it may not be helpful.

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

I have installed some 100 packages, I am happy to provide the list but it may not be helpful.

If you can't start from a clean install and then isolate the package that breaks, we can't move forward.

@2sn
Copy link
Author

2sn commented Mar 30, 2022

They are irrelevant because on my linux install the plain python script does the same thing. As noted in the earlier posts.

The MacOS note just above was only for the MacOS to test whether uniquely that one line breaks things or fixes them when commented out. It does. This was to make sure it is not just my install or platform.

@2sn
Copy link
Author

2sn commented Mar 30, 2022

@MarkWieczorek noted it dependent on other packages. I have no experience on that.

@jklymak I think you would be able to reproduce the problem if you used a non-conda vanilla Python 3.10.x install.
For Fedora, I listed the packes you may want to install (e.g., in a VM); for MacOS, please try to use homebrew.

@MarkWieczorek
Copy link

MarkWieczorek commented Mar 30, 2022

THE PROBLEM IS JUPYTER

Try this
conda create -n test python=3.10 matplotlib-base jupyter

In [1]: import matplotlib as mpl

In [2]: import matplotlib.pyplot as plt

In [3]: fig, ax = plt.subplots(1,1)

In [4]: plt.gcf().dpi
Out[4]: 100.0

In [5]: fig.tight_layout()

In [6]: plt.gcf().dpi
Out[6]: 100.0

In [7]: mpl.rcParams['savefig.format'] = 'pdf'

In [8]: fig.tight_layout()

In [9]: plt.gcf().dpi
Out[9]: 72

Note: In trying to isolate this problem, the following packages seem to work ok:

fftw>=3.3.8
liblapack>=3.8
numpy
scipy>=0.14.0
matplotlib-base>=3.3
astropy>=4.0
xarray
requests
pooch>=1.1
tqdm
palettable>=3.3
pypandoc
flake8
pip
ducc0>=0.15

@2sn
Copy link
Author

2sn commented Mar 30, 2022

export py=3.10.2
export prev=
rm Python
mkdir Python_${py}${prev}
ln -s Python_${py}${prev} Python

mkdir install
cd install
wget https://www.python.org/ftp/python/${py}/Python-${py}${prev}.tar.xz
cd
tar -xvJf install/Python-${py}${prev}.tar.xz

cd Python-${py}${prev}

./configure --prefix=$HOME/Python --enable-optimizations --enable-shared
make install

now we have to get matplotlib

~/Python>pip3 install matplotlib
Collecting matplotlib
  Using cached matplotlib-3.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 MB)
Collecting cycler>=0.10
  Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
  Using cached fonttools-4.31.2-py3-none-any.whl (899 kB)
Collecting python-dateutil>=2.7
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting numpy>=1.17
  Using cached numpy-1.22.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
Collecting packaging>=20.0
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pyparsing>=2.2.1
  Using cached pyparsing-3.0.7-py3-none-any.whl (98 kB)
Collecting pillow>=6.2.0
  Using cached Pillow-9.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB)
Collecting kiwisolver>=1.0.1
  Using cached kiwisolver-1.4.2-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB)
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pyparsing, python-dateutil, pillow, packaging, numpy, kiwisolver, fonttools, cycler, matplotlib
Successfully installed cycler-0.11.0 fonttools-4.31.2 kiwisolver-1.4.2 matplotlib-3.5.1 numpy-1.22.3 packaging-21.3 pillow-9.0.1 pyparsing-3.0.7 python-dateutil-2.8.2 six-1.16.0
WARNING: You are using pip version 21.2.4; however, version 22.0.4 is available.
You should consider upgrading via the '/home/alex/Python/bin/python3.10 -m pip install --upgrade pip' command.

and run

~>python
Python 3.10.2 (main, Mar 31 2022, 01:04:24) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([0,1])
[<matplotlib.lines.Line2D object at 0x7f4764421720>]
>>> plt.pause(.1)
>>> plt.gcf().dpi
100.0
>>> plt.tight_layout()
>>> plt.pause(.1)
>>> plt.gcf().dpi
100.0

and now with

savefig.format:    pdf 

I get as before

~>python
Python 3.10.2 (main, Mar 31 2022, 01:04:24) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot([0,1])
[<matplotlib.lines.Line2D object at 0x7f0b08e6d720>]
>>> plt.gcf().dpi
100.0
>>> plt.pause(.1)
>>> plt.tight_layout()
>>> plt.pause(.1)
>>> plt.gcf().dpi
72

xxx

So, even the most minimal (pip3 install matplotlib) install on a fresh plain python 3.10.2 breaks things.

Even with tkAgg (see plot). No Jupyter needed to break it. It's broken all by itself. I hope this helps.

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

@2sn @MarkWieczorek if either of you are able to checkout #22732 and see if that magically fixes things, that would be appreciated. I think we were falling back too soon, and should have been checking fig.canvas.get_renderer before falling back to the cached renderer... I'm still not sure why this would be so fragile to install, but I don't think this proposed change will make things worse.

(if you have gh installed: gh pr checkout 22732 in a matplotlib repo, and then pip install -e . in that directory should install it in your env).

@2sn
Copy link
Author

2sn commented Mar 30, 2022

@jklymak How would I use it (how to install from that patch)? I don't have gh (actually never heard of it before).

@jklymak
Copy link
Member

jklymak commented Mar 30, 2022

https://github.com/cli/cli/blob/trunk/docs/install_linux.md

# whatever you need to do to install `gh`
git clone -–depth 1 git@github.com:matplotlib/matplotlib.git. # you may need to use the https version here 
cd matplotlib
gh pr checkout 22732
pip install -e .

@2sn
Copy link
Author

2sn commented Mar 30, 2022

@jklymak Thanks ... seems to uninstall some existing things

Installing collected packages: pyparsing, matplotlib
  Attempting uninstall: pyparsing
    Found existing installation: pyparsing 3.0.7
    Uninstalling pyparsing-3.0.7:
      Successfully uninstalled pyparsing-3.0.7
  Attempting uninstall: matplotlib
    Found existing installation: matplotlib 3.5.1
    Uninstalling matplotlib-3.5.1:
      Successfully uninstalled matplotlib-3.5.1
  Running setup.py develop for matplotlib
Successfully installed matplotlib-3.6.0.dev1932+gbda8790ca1 pyparsing-2.4.7

and I have

In [1]: mpl.__version__
Out[1]: '3.6.0.dev1932+gbda8790ca1'

With this, the dpi issue is gone, but the plot is no longer automatically updated in pylab (ion) mode. I need to call plt.pause(...) manually to update the plot.

Python 3.10.2 (main, Jan 28 2022, 00:32:56) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.2.0 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: QtAgg
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib

In [1]: plot([0,1])
Out[1]: [<matplotlib.lines.Line2D at 0x7fd25c6a2650>]

In [2]: tight_layout()

In [3]: pause(.1)

In [4]: plt.gcf().dpi
Out[4]: 100.0

so, one bug fixed, one new ... :-(

Maybe the old pyparsing causes issues with IPython. ... No, it is not. Must be something else. It seem I just need to call plt.ion() manually now (can be accommodated in ipython config or startup file). All else works.

@tacaswell tacaswell added this to the v3.5.2 milestone Apr 4, 2022
@agallenne
Copy link

I did some conda updates (lidely including matplotlib) and I unfortunately just had the same problem displaying plots. I had to downgrade to matplotlib=3.4.3=py38hecd8cb5_0 to make it work again.

@jklymak
Copy link
Member

jklymak commented Jun 3, 2022

@agallenne please open a new issue perhaps after creating a fresh conda environment. Be sure to include all details of your setup and steps to reproduce the problem. Thanks.

@agallenne
Copy link

By doing Conda update all?

@jklymak
Copy link
Member

jklymak commented Jun 13, 2022

@agallenne
Copy link

I can't install properly matplotlib apparently. I did conda create -n py39 python=3.9 matplotlib, but I now have another error:

`Python 3.9.12 (main, Jun 1 2022, 06:36:29)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.
[TerminalIPythonApp] WARNING | Eventloop or matplotlib integration failed. Is matplotlib installed?

ImportError Traceback (most recent call last)
File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/shellapp.py:235, in InteractiveShellApp.init_gui_pylab..(key)
233 shell = self.shell
234 if self.pylab:
--> 235 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
236 key = self.pylab
237 elif self.matplotlib:

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3526, in InteractiveShell.enable_pylab(self, gui, import_all, welcome_message)
3499 """Activate pylab support at runtime.
3500
3501 This turns on support for matplotlib, preloads into the interactive
(...)
3522 This argument is ignored, no welcome message will be displayed.
3523 """
3524 from IPython.core.pylabtools import import_pylab
-> 3526 gui, backend = self.enable_matplotlib(gui)
3528 # We want to prevent the loading of pylab to pollute the user's
3529 # namespace as shown by the %who* magics, so we execute the activation
3530 # code in an empty namespace, and we update both user_ns and
3531 # user_ns_hidden with this information.
3532 ns = {}

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3487, in InteractiveShell.enable_matplotlib(self, gui)
3483 print('Warning: Cannot change to a different GUI toolkit: %s.'
3484 ' Using %s instead.' % (gui, self.pylab_gui_select))
3485 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
-> 3487 pt.activate_matplotlib(backend)
3488 configure_inline_support(self, backend)
3490 # Now we must activate the gui pylab wants to use, and fix %run to take
3491 # plot updates into account

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/pylabtools.py:357, in activate_matplotlib(backend)
352 matplotlib.rcParams['backend'] = backend
354 # Due to circular imports, pyplot may be only partially initialised
355 # when this function runs.
356 # So avoid needing matplotlib attribute-lookup to access pyplot.
--> 357 from matplotlib import pyplot as plt
359 plt.switch_backend(backend)
361 plt.show._needmain = False

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:2230, in
2228 dict.setitem(rcParams, "backend", rcsetup._auto_backend_sentinel)
2229 # Set up the backend.
-> 2230 switch_backend(rcParams["backend"])
2232 # Just to be safe. Interactive mode can be turned on without
2233 # calling plt.ion() so register it again here.
2234 # This is safe because multiple calls to install_repl_displayhook
2235 # are no-ops and the registered function respect mpl.is_interactive()
2236 # to determine if they should trigger a draw.
2237 install_repl_displayhook()

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:267, in switch_backend(newbackend)
260 # Backends are implemented as modules, but "inherit" default method
261 # implementations from backend_bases._Backend. This is achieved by
262 # creating a "class" that inherits from backend_bases._Backend and whose
263 # body is filled with the module's globals.
265 backend_name = cbook._backend_module_name(newbackend)
--> 267 class backend_mod(matplotlib.backend_bases._Backend):
268 locals().update(vars(importlib.import_module(backend_name)))
270 required_framework = _get_required_interactive_framework(backend_mod)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:268, in switch_backend..backend_mod()
267 class backend_mod(matplotlib.backend_bases._Backend):
--> 268 locals().update(vars(importlib.import_module(backend_name)))

File ~/opt/anaconda3/envs/py39/lib/python3.9/importlib/init.py:127, in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py:5, in
1 """
2 Render to qt from agg
3 """
----> 5 from .backend_qtagg import (
6 _BackendQTAgg, FigureCanvasQTAgg, FigureManagerQT, NavigationToolbar2QT,
7 backend_version, FigureCanvasAgg, FigureCanvasQT
8 )
11 @_BackendQTAgg.export
12 class _BackendQT5Agg(_BackendQTAgg):
13 pass

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py:9, in
5 import ctypes
7 from matplotlib.transforms import Bbox
----> 9 from .qt_compat import QT_API, _enum, _setDevicePixelRatio
10 from .. import cbook
11 from .backend_agg import FigureCanvasAgg

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py:128, in
126 break
127 else:
--> 128 raise ImportError("Failed to import any qt binding")
129 else: # We should not get there.
130 raise AssertionError(f"Unexpected QT_API: {QT_API}")

ImportError: Failed to import any qt binding
[TerminalIPythonApp] WARNING | Unknown error in handling startup files:

ModuleNotFoundError Traceback (most recent call last)
File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/shellapp.py:360, in InteractiveShellApp._exec_file(self, fname, shell_futures)
356 self.shell.safe_execfile_ipy(full_filename,
357 shell_futures=shell_futures)
358 else:
359 # default to python, even without extension
--> 360 self.shell.safe_execfile(full_filename,
361 self.shell.user_ns,
362 shell_futures=shell_futures,
363 raise_exceptions=True)
364 finally:
365 sys.argv = save_argv

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2737, in InteractiveShell.safe_execfile(self, fname, exit_ignore, raise_exceptions, shell_futures, *where)
2735 try:
2736 glob, loc = (where + (None, ))[:2]
-> 2737 py3compat.execfile(
2738 fname, glob, loc,
2739 self.compile if shell_futures else None)
2740 except SystemExit as status:
2741 # If the call was made with 0 or None exit status (sys.exit(0)
2742 # or sys.exit() ), don't bother showing a traceback, as both of
(...)
2748 # For other exit status, we show the exception unless
2749 # explicitly silenced, but only in short form.
2750 if status.code:

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/IPython/utils/py3compat.py:55, in execfile(fname, glob, loc, compiler)
53 with open(fname, "rb") as f:
54 compiler = compiler or compile
---> 55 exec(compiler(f.read(), fname, "exec"), glob, loc)

File ~/.ipython/profile_default/startup/startup_funtions.py:7, in
4 pass
6 import astropy.units as units
----> 7 from uncertainties import ufloat, umath

ModuleNotFoundError: No module named 'uncertainties'`

@tacaswell
Copy link
Member

This file is run as part of IPython start up, but is controlled by you:

File ~/.ipython/profile_default/startup/startup_funtions.py:7, in
4 pass
6 import astropy.units as units
----> 7 from uncertainties import ufloat, umath

You need to keep installing things until you have all of the libraries you need or remove stuff from your start up file if you no longer need it.

This discussion is probably better had at https://discourse.matplotlib.org rather than the issue tracker.

@agallenne
Copy link

agallenne commented Jun 13, 2022

yes I know, sorry I pasted everything, it was more the first lines actually, saying "[TerminalIPythonApp] WARNING | Eventloop or matplotlib integration failed. Is matplotlib installed?"

Then if I want to plot I can't because if I do "import matplotlib.pyplot as plt" I have this:

In [2]: import matplotlib.pyplot as plt

ImportError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 import matplotlib.pyplot as plt

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:2230, in
2228 dict.setitem(rcParams, "backend", rcsetup._auto_backend_sentinel)
2229 # Set up the backend.
-> 2230 switch_backend(rcParams["backend"])
2232 # Just to be safe. Interactive mode can be turned on without
2233 # calling plt.ion() so register it again here.
2234 # This is safe because multiple calls to install_repl_displayhook
2235 # are no-ops and the registered function respect mpl.is_interactive()
2236 # to determine if they should trigger a draw.
2237 install_repl_displayhook()

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:267, in switch_backend(newbackend)
260 # Backends are implemented as modules, but "inherit" default method
261 # implementations from backend_bases._Backend. This is achieved by
262 # creating a "class" that inherits from backend_bases._Backend and whose
263 # body is filled with the module's globals.
265 backend_name = cbook._backend_module_name(newbackend)
--> 267 class backend_mod(matplotlib.backend_bases._Backend):
268 locals().update(vars(importlib.import_module(backend_name)))
270 required_framework = _get_required_interactive_framework(backend_mod)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:268, in switch_backend..backend_mod()
267 class backend_mod(matplotlib.backend_bases._Backend):
--> 268 locals().update(vars(importlib.import_module(backend_name)))

File ~/opt/anaconda3/envs/py39/lib/python3.9/importlib/init.py:127, in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py:5, in
1 """
2 Render to qt from agg
3 """
----> 5 from .backend_qtagg import (
6 _BackendQTAgg, FigureCanvasQTAgg, FigureManagerQT, NavigationToolbar2QT,
7 backend_version, FigureCanvasAgg, FigureCanvasQT
8 )
11 @_BackendQTAgg.export
12 class _BackendQT5Agg(_BackendQTAgg):
13 pass

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py:9, in
5 import ctypes
7 from matplotlib.transforms import Bbox
----> 9 from .qt_compat import QT_API, _enum, _setDevicePixelRatio
10 from .. import cbook
11 from .backend_agg import FigureCanvasAgg

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py:128, in
126 break
127 else:
--> 128 raise ImportError("Failed to import any qt binding")
129 else: # We should not get there.
130 raise AssertionError(f"Unexpected QT_API: {QT_API}")

ImportError: Failed to import any qt binding

In [3]: import matplotlib

In [4]: import matplotlib.pyplot as plt

ImportError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 import matplotlib.pyplot as plt

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:2230, in
2228 dict.setitem(rcParams, "backend", rcsetup._auto_backend_sentinel)
2229 # Set up the backend.
-> 2230 switch_backend(rcParams["backend"])
2232 # Just to be safe. Interactive mode can be turned on without
2233 # calling plt.ion() so register it again here.
2234 # This is safe because multiple calls to install_repl_displayhook
2235 # are no-ops and the registered function respect mpl.is_interactive()
2236 # to determine if they should trigger a draw.
2237 install_repl_displayhook()

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:267, in switch_backend(newbackend)
260 # Backends are implemented as modules, but "inherit" default method
261 # implementations from backend_bases._Backend. This is achieved by
262 # creating a "class" that inherits from backend_bases._Backend and whose
263 # body is filled with the module's globals.
265 backend_name = cbook._backend_module_name(newbackend)
--> 267 class backend_mod(matplotlib.backend_bases._Backend):
268 locals().update(vars(importlib.import_module(backend_name)))
270 required_framework = _get_required_interactive_framework(backend_mod)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/pyplot.py:268, in switch_backend..backend_mod()
267 class backend_mod(matplotlib.backend_bases._Backend):
--> 268 locals().update(vars(importlib.import_module(backend_name)))

File ~/opt/anaconda3/envs/py39/lib/python3.9/importlib/init.py:127, in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py:5, in
1 """
2 Render to qt from agg
3 """
----> 5 from .backend_qtagg import (
6 _BackendQTAgg, FigureCanvasQTAgg, FigureManagerQT, NavigationToolbar2QT,
7 backend_version, FigureCanvasAgg, FigureCanvasQT
8 )
11 @_BackendQTAgg.export
12 class _BackendQT5Agg(_BackendQTAgg):
13 pass

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py:9, in
5 import ctypes
7 from matplotlib.transforms import Bbox
----> 9 from .qt_compat import QT_API, _enum, _setDevicePixelRatio
10 from .. import cbook
11 from .backend_agg import FigureCanvasAgg

File ~/opt/anaconda3/envs/py39/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py:128, in
126 break
127 else:
--> 128 raise ImportError("Failed to import any qt binding")
129 else: # We should not get there.
130 raise AssertionError(f"Unexpected QT_API: {QT_API}")

ImportError: Failed to import any qt binding

@agallenne
Copy link

Ok I installed PyQt5, let's see steps further

@agallenne
Copy link

No, same problem with a fresh env

@jklymak
Copy link
Member

jklymak commented Jun 13, 2022

This is not an appropriate forum for user help. Please take to discourse.matplotlib.org and we can try and help. Thanks for your understanding!

@agallenne
Copy link

Not sure to understand. I am not asking for help about installing matplotlib. I just add problem to have a fresh py39 install (as you asked). But I figured it out and saw that I still have the problem with tight_layout, as the original post.

@jklymak
Copy link
Member

jklymak commented Jun 14, 2022

I you have this issue, and you have a fresh install, please open a new issue with all the relevant details. The dev team cannot reproduce the original issue, so please be sure to give us the magic formula to make it happen. (Also please have a look at how to format code on GitHub: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#quoting-code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs clarification Issues that need more information to resolve.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants