tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

Compatibility issue with matplotlib Legend _ncol vs _ncols

Open JohannesMaierhofer opened this issue 3 years ago • 2 comments
trafficstars

in _legend.py

if obj._ncol != 1:
        data["current axes"].axis_options.append(f"legend columns={obj._ncol}")

leads to AttributeError: 'Legend' object has no attribute '_ncol'

Fix: replace with _ncols

JohannesMaierhofer avatar Sep 29 '22 08:09 JohannesMaierhofer

This issue came up with an internal change in matplotlib 3.6, so all newer versions of matplotlib will not work with tikzplotlib out of the box.

Whilst #558 is fixing the issue, it does not seem like tikzplotlib would get a new release anytime soon... :disappointed:

In the meantime, the following workaround lets you use tikzplotlib with matplotlib >= 3.6:

def tikzplotlib_fix_ncols(obj):
    """
    workaround for matplotlib 3.6 renamed legend's _ncol to _ncols, which breaks tikzplotlib
    """
    if hasattr(obj, "_ncols"):
        obj._ncol = obj._ncols
    for child in obj.get_children():
        tikzplotlib_fix_ncols(child)

You then need to call tikzplotlib_fix_ncols(fig) with the figure object (e.g. fig = plt.figure() for a new one, or fig = plt.gcf() for the currently-active figure) before passing the figure to tikzplotlib.save.

st-- avatar Jan 24 '23 07:01 st--