Skip to content

[Bug]: Any operation on matplotlib fails due to gtk4 backend error #25602

Closed

Description

@mikayahlevi

Bug summary

This may be an Arch Linux dependency issue, but when using GTK4 as the backend any operation that involves a window will crash the Python program.

Code for reproduction

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 1450, in subplots
    fig = figure(**fig_kw)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/_api/deprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 783, in figure
    manager = new_figure_manager(
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 359, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 3513, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 3518, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1703, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 2830, in create_with_canvas
    return cls(canvas_class(figure), num)
  File "/home/mikayah/.local/lib/python3.10/site-packages/matplotlib/backends/backend_gtk4.py", line 79, in __init__
    **css.load_from_data(b".matplotlib-canvas { background-color: white; }")
TypeError: Gtk.CssProvider.load_from_data() takes exactly 3 arguments (2 given)**

Expected outcome

Not crashing

Additional information

No response

Operating system

Arch

Matplotlib Version

3.6.3

Matplotlib Backend

GTK4Agg

Python version

3.10.10

Jupyter version

No response

Installation

Linux package manager

Activity

  1. oscargus commented on Apr 3, 2023

    @oscargus
    Member

    Strange. I am in no way an expert on this, but did a quick search and in the C/C++ interface load_from_data takes another argument, the length of the "string" (byte-sequence). However, the Python binding only takes the "string": https://amolenaar.github.io/pgi-docgen/#Gtk-4.0/classes/CssProvider.html#Gtk.CssProvider.load_from_data

  2. oscargus commented on Apr 3, 2023

    @oscargus
    Member

    On the other hand there was a fix in #25280 that seems very related. This was merged into 3.7.1, so if you get hold of the latest Matplotlib version, it should work!(?)

    if Gtk.check_version(4, 9, 3) is None:
    css.load_from_data(style, -1)
    else:
    css.load_from_data(style.encode('utf-8'))

  3. ksunden commented on Apr 3, 2023

    @ksunden
    Member

    Given this does appear to be addressed on main (and even a release) already, as @oscargus points out, I'll go ahead and close this, feel free to re-ping us/reopen if that does not actually address the issue.

  4. added this to the v3.7.1 milestone on Jul 5, 2023
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions