Skip to content

Commit 72cde2a

Browse files
added _simplifyStairs
1 parent b73691a commit 72cde2a

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

test/test_cleanfigure.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,41 @@ def test_simplifyLine():
246246
assert l.get_ydata().shape == (2,)
247247

248248

249+
# def test_simplifyStairs():
250+
# """octave code
251+
252+
# ```octave
253+
# %% example 4
254+
255+
# addpath ("../matlab2tikz/src")
256+
257+
# x = linspace(1, 100, 20);
258+
# y1 = linspace(1, 100, 20);
259+
260+
# figure
261+
# stairs(x, y1)
262+
# xlim([20, 80])
263+
# ylim([20, 80])
264+
# set(gcf,'Units','Inches');
265+
# set(gcf,'Position',[2.5 2.5 5 5])
266+
# cleanfigure;
267+
# ```
268+
# """
269+
# # TODO: it looks like matlab changes the data to be plotted when using `stairs` command,
270+
# # whereas matplotlib stores the same data but displays it as a step.
271+
# x = np.linspace(1, 100, 20)
272+
# y = np.linspace(1, 100, 20)
273+
274+
# with plt.rc_context(rc=RC_PARAMS):
275+
# fig, ax = plt.subplots(1, 1, figsize=(5, 5))
276+
# (l,) = ax.step(x, y, where="post")
277+
# ax.set_ylim([20, 80])
278+
# ax.set_xlim([20, 80])
279+
# cleanfigure.pruneOutsideBox(fig, ax, l)
280+
# cleanfigure.movePointscloser(fig, ax, l)
281+
# cleanfigure.simplifyStairs(fig, ax, l)
282+
283+
249284
def test_limitPrecision():
250285
"""
251286
octave code

tikzplotlib/cleanfigure.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,42 @@ def _simplifyLine(fighandle, axhandle, linehandle, target_resolution):
812812
_removeData(linehandle, id_remove)
813813

814814

815-
removeData(linehandle, id_remove)
815+
def _simplifyStairs(fighandle, axhandle, linehandle):
816+
# TODO: it looks like matlab changes the data to be plotted when using `stairs` command, whereas matplotlib stores the same data but displays it as a step.
816817

818+
xData = linehandle.get_xdata()
819+
yData = linehandle.get_ydata()
820+
data = np.stack([xData, yData], 1)
821+
if _isempty(xData) or _isempty(yData):
822+
return
823+
824+
xNoDiff = np.concatenate([np.array([False]).reshape((-1,) ), _diff(xData) == 0])
825+
yNoDiff = np.concatenate([np.array([False]).reshape((-1,) ), _diff(yData) == 0])
817826

818-
def simplifyStairs(fighandle, axhandle, linehandle):
819-
# TODO: implement this
820-
raise NotImplementedError
827+
xNoDiff[-1] = False
828+
yNoDiff[-1] = False
829+
830+
xIsMonotone = np.concatenate(
831+
[
832+
np.array([True]).reshape((-1,) ),
833+
_diff(np.sign(_diff(xData))) == 0,
834+
np.array([True]).reshape((-1,) )
835+
]
836+
)
837+
yIsMonotone = np.concatenate(
838+
[
839+
np.array([True]).reshape((-1,) ),
840+
_diff(np.sign(_diff(yData))) == 0,
841+
np.array([True]).reshape((-1,) )
842+
]
843+
)
844+
xRemove = np.logical_and(xNoDiff, yIsMonotone)
845+
yRemove = np.logical_and(yNoDiff, xIsMonotone)
846+
847+
id_remove = np.argwhere(xRemove or yRemove)
848+
new_data = _removeData(data, id_remove)
849+
linehandle.set_xdata(new_data[:, 0])
850+
linehandle.set_ydata(new_data[:, 1])
821851

822852

823853
def _pixelate(x, y, xToPix, yToPix):

0 commit comments

Comments
 (0)