tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

`clean_figure()` breaks on inverted axes.

Open VitruvianSasquatch opened this issue 2 years ago • 0 comments
trafficstars

Attempting to clean a figure with an inverted axis cascades into the error

IndexError: index 0 is out of bounds for axis 0 with size 0

This is due to assuming that the smallest value of xLim or yLim is always at index 0.

MWE:

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib as tpl

t = np.linspace(-10, 10, 100)
x = np.sin(t)/t

plt.plot(t, x)
plt.gca().invert_yaxis();
print(len(tpl.get_tikz_code()))
tpl.clean_figure();
print(len(tpl.get_tikz_code()))

Fix:

On lines 525 & 526, replace

maskX = np.logical_and(data[:, 0] > xLim[0], data[:, 0] < xLim[1])
maskY = np.logical_and(data[:, 1] > yLim[0], data[:, 1] < yLim[1])

with

maskX = np.logical_and(data[:, 0] > np.min(xLim), data[:, 0] < np.max(xLim))
maskY = np.logical_and(data[:, 1] > np.min(yLim), data[:, 1] < np.max(yLim))

On lines 833, 834, replace

xRange = xLim[1] - xLim[0]
yRange = yLim[1] - yLim[0]

with

xRange = np.max(xLim) - np.min(xLim)
yRange = np.max(yLim) - np.min(yLim)

VitruvianSasquatch avatar Dec 09 '22 03:12 VitruvianSasquatch