@@ -277,7 +277,7 @@ def _get_visual_limits(fighandle, axhandle):
277
277
return xLim , yLim
278
278
279
279
280
- def _replace_data_with_NaN (linehandle , id_replace ):
280
+ def _replace_data_with_NaN (data , id_replace , is3D ):
281
281
"""Replaces data at id_replace with NaNs.
282
282
283
283
:param data: array of x and y data with shape [N, 2]
@@ -287,34 +287,44 @@ def _replace_data_with_NaN(linehandle, id_replace):
287
287
:param linehandle: matplotlib line handle object
288
288
:type linehandle: object
289
289
"""
290
- if _elements ( id_replace ) == 0 :
291
- return
290
+ if _isempty ( data ) :
291
+ return data
292
292
293
- is3D = _lineIs3D (linehandle )
293
+ # is3D = _lineIs3D(linehandle)
294
294
295
+ # if is3D:
296
+ # xData, yData, zData = linehandle.get_data_3d()
297
+ # zData = zData.copy()
298
+ # else:
299
+ # xData = linehandle.get_xdata().astype(np.float32)
300
+ # yData = linehandle.get_ydata().astype(np.float32)
295
301
if is3D :
296
- xData , yData , zData = linehandle .get_data_3d ()
297
- zData = zData .copy ()
302
+ xData , yData , zData = _split_data_3D (data )
298
303
else :
299
- xData = linehandle .get_xdata ().astype (np .float32 )
300
- yData = linehandle .get_ydata ().astype (np .float32 )
304
+ xData , yData = _split_data_2D (data )
301
305
302
306
xData [id_replace ] = np .NaN
303
307
yData [id_replace ] = np .NaN
304
308
if is3D :
305
309
zData = zData .copy ()
306
310
zData [id_replace ] = np .NaN
307
311
312
+ # if is3D:
313
+ # # TODO: I don't understand why I need to set both to get tikz code reduction to work
314
+ # linehandle.set_data_3d(xData, yData, zData)
315
+ # linehandle.set_data(xData, yData)
316
+ # else:
317
+ # linehandle.set_xdata(xData)
318
+ # linehandle.set_ydata(yData)
319
+
308
320
if is3D :
309
- # TODO: I don't understand why I need to set both to get tikz code reduction to work
310
- linehandle .set_data_3d (xData , yData , zData )
311
- linehandle .set_data (xData , yData )
321
+ new_data = _stack_data_3D (xData , yData , zData )
312
322
else :
313
- linehandle . set_xdata (xData )
314
- linehandle . set_ydata ( yData )
323
+ new_data = _stack_data_2D (xData , yData )
324
+ return new_data
315
325
316
326
317
- def _remove_data (linehandle , id_remove ):
327
+ def _remove_data (data , id_remove , is3D ):
318
328
"""remove data at id_remove
319
329
320
330
:param data: array of x and y data with shape [N, 2]
@@ -324,22 +334,25 @@ def _remove_data(linehandle, id_remove):
324
334
:param linehandle: matplotlib linehandle object
325
335
:type linehandle: object
326
336
"""
327
- if _elements ( id_remove ) == 0 :
328
- return
337
+ if _isempty ( data ) :
338
+ return data
329
339
330
- is3D = _lineIs3D (linehandle )
331
340
if is3D :
332
- xData , yData , zData = linehandle . get_data_3d ( )
341
+ xData , yData , zData = _split_data_3D ( data )
333
342
else :
334
- xData = linehandle .get_xdata ().astype (np .float32 )
335
- yData = linehandle .get_ydata ().astype (np .float32 )
343
+ xData , yData = _split_data_2D (data )
336
344
337
345
xData = np .delete (xData , id_remove , axis = 0 )
338
346
yData = np .delete (yData , id_remove , axis = 0 )
339
347
if is3D :
340
348
zData = np .delete (zData , id_remove , axis = 0 )
341
349
342
350
if is3D :
351
+ newdata = _stack_data_3D (xData , yData , zData )
352
+ else :
353
+ newdata = _stack_data_2D (xData , yData )
354
+ return newdata
355
+
343
356
# TODO: I don't understand why I need to set both to get tikz code reduction to work
344
357
linehandle .set_data_3d (xData , yData , zData )
345
358
linehandle .set_data (xData , yData )
@@ -348,6 +361,26 @@ def _remove_data(linehandle, id_remove):
348
361
linehandle .set_ydata (yData )
349
362
350
363
364
+ def _split_data_2D (data ):
365
+ xData , yData = np .split (data , 2 , axis = 1 )
366
+ return xData .reshape ((- 1 ,)), yData .reshape ((- 1 ,))
367
+
368
+
369
+ def _stack_data_2D (xData , yData ):
370
+ data = np .stack ([xData , yData ], axis = 1 )
371
+ return data
372
+
373
+
374
+ def _split_data_3D (data ):
375
+ xData , yData , zData = np .split (data , 3 , axis = 1 )
376
+ return yData .reshape ((- 1 ,)), yData .reshape ((- 1 ,)), zData .reshape ((- 1 ,))
377
+
378
+
379
+ def _stack_data_3D (xData , yData , zData ):
380
+ data = np .stack ([xData , yData , zData ], axis = 1 )
381
+ return data
382
+
383
+
351
384
def _diff (x , * args , ** kwargs ):
352
385
"""modification of np.diff(x, *args, **kwargs).
353
386
- If x is empty, return np.array([False])
@@ -364,20 +397,11 @@ def _diff(x, *args, **kwargs):
364
397
return np .diff (x , * args , ** kwargs )
365
398
366
399
367
- def _remove_NaNs (linehandle ):
400
+ def _remove_NaNs (data ):
368
401
"""Removes superflous NaNs in the data, i.e. those at the end/beginning of the data and consecutive ones.
369
402
370
403
:param linehandle: matplotlib linehandle object
371
404
"""
372
- is3D = _lineIs3D (linehandle )
373
- if is3D :
374
- xData , yData , zData = linehandle .get_data_3d ()
375
- data = np .stack ([xData , yData , zData ], axis = 1 )
376
- else :
377
- xData = linehandle .get_xdata ().astype (np .float32 )
378
- yData = linehandle .get_ydata ().astype (np .float32 )
379
- data = np .stack ([xData , yData ], axis = 1 )
380
-
381
405
id_nan = np .any (np .isnan (data ), axis = 1 )
382
406
id_remove = np .argwhere (id_nan ).reshape ((- 1 ,))
383
407
if _isempty (id_remove ):
@@ -394,20 +418,13 @@ def _remove_NaNs(linehandle):
394
418
395
419
if _isempty (id_first ):
396
420
# remove entire data
397
- id_remove = np .arange (len (xData ))
421
+ id_remove = np .arange (len (data ))
398
422
else :
399
423
id_remove = np .concatenate (
400
- [np .arange (0 , id_first ), id_remove , np .arange (id_last + 1 , len (xData ))]
424
+ [np .arange (0 , id_first ), id_remove , np .arange (id_last + 1 , len (data ))]
401
425
)
402
426
data = np .delete (data , id_remove , axis = 0 )
403
-
404
- if is3D :
405
- # TODO: I don't understand why I need to set both to get tikz code reduction to work
406
- linehandle .set_data_3d (data [:, 0 ], data [:, 1 ], data [:, 2 ])
407
- linehandle .set_data (xData , yData )
408
- else :
409
- linehandle .set_xdata (data [:, 0 ])
410
- linehandle .set_ydata (data [:, 1 ])
427
+ return data
411
428
412
429
413
430
def _isInBox (data , xLim , yLim ):
@@ -443,6 +460,18 @@ def _axIs3D(axhandle):
443
460
return hasattr (axhandle , "get_zlim" )
444
461
445
462
463
+ def _get_data (linehandle ):
464
+ is3D = _lineIs3D (linehandle )
465
+ if is3D :
466
+ xData , yData , zData = linehandle .get_data_3d ()
467
+ data = _stack_data_3D (xData , yData , zData )
468
+ else :
469
+ xData = linehandle .get_xdata ().astype (np .float32 )
470
+ yData = linehandle .get_ydata ().astype (np .float32 )
471
+ data = _stack_data_2D (xData , yData )
472
+ return data
473
+
474
+
446
475
def _get_visual_data (axhandle , linehandle ):
447
476
"""Returns the visual representation of the data,
448
477
respecting possible log_scaling and projection into the image plane.
@@ -502,7 +531,14 @@ def _isempty(array):
502
531
return _elements (array ) == 0
503
532
504
533
505
- def _prune_outside_box (fighandle , axhandle , linehandle ):
534
+ def _line_has_lines (linehandle ):
535
+ hasLines = (linehandle .get_linestyle () is not None ) and (
536
+ linehandle .get_linewidth () > 0.0
537
+ )
538
+ return hasLines
539
+
540
+
541
+ def _prune_outside_box (xLim , yLim , data , visual_data , is3D , hasLines ):
506
542
"""Some sections of the line may sit outside of the visible box. Cut those off.
507
543
508
544
This method is not pure because it updates the linehandle object's data.
@@ -514,28 +550,28 @@ def _prune_outside_box(fighandle, axhandle, linehandle):
514
550
:param linehandle: matplotlib line2D handle object
515
551
:type linehandle: obj
516
552
"""
517
- xData , yData = _get_visual_data (axhandle , linehandle )
553
+ # xData, yData = _get_visual_data(axhandle, linehandle)
518
554
519
- data = np .stack ([xData , yData ], axis = 1 )
555
+ # data = np.stack([xData, yData], axis=1)
520
556
521
- if _elements (data ) == 0 :
522
- return
557
+ if _elements (visual_data ) == 0 :
558
+ return data
523
559
524
- hasLines = (linehandle .get_linestyle () is not None ) and (
525
- linehandle .get_linewidth () > 0.0
526
- )
560
+ # hasLines = (linehandle.get_linestyle() is not None) and (
561
+ # linehandle.get_linewidth() > 0.0
562
+ # )
527
563
528
- xLim , yLim = _get_visual_limits (fighandle , axhandle )
564
+ # xLim, yLim = _get_visual_limits(fighandle, axhandle)
529
565
530
566
tol = 1.0e-10
531
567
relaxedXLim = xLim + np .array ([- tol , tol ])
532
568
relaxedYLim = yLim + np .array ([- tol , tol ])
533
569
534
- dataIsInBox = _isInBox (data , relaxedXLim , relaxedYLim )
570
+ dataIsInBox = _isInBox (visual_data , relaxedXLim , relaxedYLim )
535
571
536
572
shouldPlot = dataIsInBox
537
573
if hasLines :
538
- segvis = _segment_visible (data , dataIsInBox , xLim , yLim )
574
+ segvis = _segment_visible (visual_data , dataIsInBox , xLim , yLim )
539
575
shouldPlot = np .logical_or (
540
576
shouldPlot , np .concatenate ([np .array ([False ]).reshape ((- 1 ,)), segvis ])
541
577
)
@@ -558,9 +594,11 @@ def _prune_outside_box(fighandle, axhandle, linehandle):
558
594
559
595
id_replace = id_remove [idx ]
560
596
id_remove = id_remove [np .logical_not (idx )]
561
- _replace_data_with_NaN (linehandle , id_replace )
562
- _remove_data (linehandle , id_remove )
563
- _remove_NaNs (linehandle )
597
+
598
+ data = _replace_data_with_NaN (data , id_replace , is3D )
599
+ data = _remove_data (data , id_remove , is3D )
600
+ data = _remove_NaNs (data )
601
+ return data
564
602
565
603
566
604
def _move_points_closer (fighandle , axhandle , linehandle ):
0 commit comments