Skip to content

Commit ac9bdc7

Browse files
cleanfigure
* pixelate * added two missing lines in pruneOutsideBox which caused an error * added plot type check in cleanfigure function
1 parent 8daef84 commit ac9bdc7

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

tikzplotlib/cleanfigure.py

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import matplotlib
23
from matplotlib import pyplot as plt
34

45

@@ -28,8 +29,19 @@ def cleanfigure(fighandle=None, axhandle=None, target_resolution=600, scalePreci
2829
elif (fighandle is not None) and (axhandle is None):
2930
axhandle = fighandle.axes[0]
3031

32+
# Note: ax.scatter and ax.plot create Line2D objects in a property ax.lines
33+
# ax.bar creates BarContainer objects in a property ax.bar
34+
3135
for linehandle in axhandle.lines:
32-
cleanline(fighandle, axhandle, linehandle, target_resolution, scalePrecision)
36+
if type(linehandle) == matplotlib.lines.Line2D:
37+
cleanline(fighandle, axhandle, linehandle, target_resolution, scalePrecision)
38+
else:
39+
raise NotImplementedError
40+
41+
for container in axhandle.containers:
42+
if type(container) == matplotlib.container.BarContainer:
43+
pass
44+
3345

3446

3547
def cleanline(fighandle, axhandle, linehandle, target_resolution, scalePrecision):
@@ -250,10 +262,12 @@ def elements(array):
250262
"""
251263
return array.ndim and array.size
252264

265+
253266
def isempty(array):
254267
"""proxy for matlab / octave isempty function"""
255268
return elements(array) == 0
256269

270+
257271
def pruneOutsideBox(fighandle, axhandle, linehandle):
258272
"""Some sections of the line may sit outside of the visible box. Cut those off.
259273
@@ -300,6 +314,9 @@ def pruneOutsideBox(fighandle, axhandle, linehandle):
300314
shouldPlot, np.concatenate([segvis, np.array([False]).reshape((-1,))])
301315
)
302316

317+
id_replace = np.array([[]])
318+
id_remove = np.array([[]])
319+
303320
if not np.all(shouldPlot):
304321
id_remove = np.argwhere(np.logical_not(shouldPlot))
305322

@@ -593,9 +610,8 @@ def simplifyLine(fighandle, axhandle, linehandle, target_resolution):
593610
if hasMarkers and not hasLines:
594611
# Pixelate data at the zoom multiplier
595612
# TODO implement this
596-
# mask = pixelate(xData, yData, xToPix, yToPix);
597-
# id_remove = find(mask==0);
598-
raise NotImplementedError
613+
mask = pixelate(xData, yData, xToPix, yToPix)
614+
id_remove = np.argwhere(mask*1 == 0)
599615
elif hasLines and not hasMarkers:
600616
# Get the width of a pixel
601617
xPixelWidth = 1 / xToPix
@@ -645,6 +661,47 @@ def simplifyLine(fighandle, axhandle, linehandle, target_resolution):
645661
linehandle.set_ydata(data[:, 1])
646662

647663

664+
665+
666+
def pixelate(x, y, xToPix, yToPix):
667+
"""Rough reduction of data points at a multiple of the target resolution.
668+
The resolution is lost only beyond the multiplier magnification.
669+
670+
Parameters
671+
----------
672+
x : [type]
673+
[description]
674+
y : [type]
675+
[description]
676+
xToPix : [type]
677+
[description]
678+
yToPix : [type]
679+
[description]
680+
681+
Returns
682+
-------
683+
np.ndarray
684+
boolean mask
685+
"""
686+
# TODO: implement this
687+
mult = 2
688+
dataPixel = np.round(np.stack([x * xToPix * mult, y * yToPix * mult], axis=1))
689+
id_orig = np.argsort(dataPixel[:, 0])
690+
dataPixelSorted = dataPixel[id_orig, :]
691+
692+
m = np.logical_or(np.diff(dataPixelSorted[:, 0]) != 0, np.diff(dataPixelSorted[:, 1]) != 0)
693+
mask_sorted = np.concatenate([np.array([True]).reshape((-1, )), m], axis=0)
694+
695+
mask = np.ones((x.shape)) == 0
696+
mask[id_orig] = mask_sorted
697+
mask[0] = True
698+
mask[-1] = True
699+
700+
isnan = np.logical_or(np.isnan(x), np.isnan(y))
701+
mask[isnan] = True
702+
return mask
703+
704+
648705
def getWidthHeightInPixels(fighandle, target_resolution):
649706
"""Target resolution as ppi / dpi. Return width and height in pixels
650707
@@ -766,6 +823,8 @@ def opheimSimplify(x, y, tol):
766823
return mask
767824

768825

826+
827+
769828
def limitPrecision(fighandle, axhandle, linehandle, alpha):
770829
"""Limit the precision of the given data. If alpha is 0 or negative do nothing.
771830
@@ -823,6 +882,8 @@ def limitPrecision(fighandle, axhandle, linehandle, alpha):
823882
linehandle.set_ydata(data[:, 1])
824883

825884

885+
886+
826887
def segmentVisible(data, dataIsInBox, xLim, yLim):
827888
"""Given a bounding box {x,y}Lim, determine whether the line between all
828889
pairs of subsequent data points [data(idx,:)<-->data(idx+1,:)] is visible.
@@ -902,6 +963,8 @@ def corners2D(xLim, yLim):
902963
return bottomLeft, topLeft, bottomRight, topRight
903964

904965

966+
967+
905968
def segmentsIntersect(X1, X2, X3, X4):
906969
"""Checks whether the segments X1--X2 and X3--X4 intersect.
907970
@@ -927,7 +990,7 @@ def segmentsIntersect(X1, X2, X3, X4):
927990

928991

929992
def crossLines(X1, X2, X3, X4):
930-
"""
993+
"""
931994
Checks whether the segments X1--X2 and X3--X4 intersect.
932995
See https://en.wikipedia.org/wiki/Line-line_intersection for reference.
933996
Given four points X_k=(x_k,y_k), k\in{1,2,3,4}, and the two lines

0 commit comments

Comments
 (0)