66from PySide6 .QtCore import QThreadPool , QTimer , Signal , Qt
77from PySide6 .QtWidgets import QFileDialog , QMessageBox , QProgressDialog
88
9- from matplotlib .artist import Artist
109from matplotlib .axes import Axes
1110from matplotlib .backends .backend_qtagg import FigureCanvas
1211from matplotlib .figure import Figure
4443 q_to_tth ,
4544 tth_to_q ,
4645)
46+ from hexrdgui .utils .matplotlib import remove_artist
4747from hexrdgui .utils .tth_distortion import apply_tth_distortion_if_needed
4848from hexrdgui .waterfall_plot import WaterfallPlotDialog
4949
@@ -249,24 +249,24 @@ def clear_azimuthal_integral_axis(self):
249249
250250 def clear_wppf_plot (self ):
251251 if self .wppf_plot :
252- self .wppf_plot . remove ( )
252+ remove_artist ( self .wppf_plot )
253253 self .wppf_plot = None
254254
255255 if self .wppf_background_plot :
256- self .wppf_background_plot . remove ( )
256+ remove_artist ( self .wppf_background_plot )
257257 self .wppf_background_plot = None
258258
259259 if self .wppf_amorphous_plot :
260- self .wppf_amorphous_plot . remove ( )
260+ remove_artist ( self .wppf_amorphous_plot )
261261 self .wppf_amorphous_plot = None
262262
263263 if self .wppf_difference_plot :
264- self .wppf_difference_plot . remove ( )
264+ remove_artist ( self .wppf_difference_plot )
265265 self .wppf_difference_plot = None
266266
267267 def clear_auto_picked_data_artists (self ):
268268 while self .auto_picked_data_artists :
269- self .auto_picked_data_artists .pop (0 ). remove ( )
269+ remove_artist ( self .auto_picked_data_artists .pop (0 ))
270270
271271 def load_images (self , image_names ):
272272 HexrdConfig ().emit_update_status_bar ('Loading image view...' )
@@ -852,7 +852,7 @@ def update_overlays(self):
852852
853853 def clear_detector_borders (self ):
854854 while self .cached_detector_borders :
855- _safe_remove_artist (self .cached_detector_borders .pop (0 ))
855+ remove_artist (self .cached_detector_borders .pop (0 ))
856856
857857 self .draw_idle ()
858858
@@ -876,7 +876,7 @@ def draw_detector_borders(self):
876876
877877 def clear_stereo_border_artists (self ):
878878 while self .stereo_border_artists :
879- _safe_remove_artist (self .stereo_border_artists .pop (0 ))
879+ remove_artist (self .stereo_border_artists .pop (0 ))
880880
881881 self .draw_idle ()
882882
@@ -917,7 +917,7 @@ def draw_auto_picked_data(self):
917917
918918 def clear_saturation (self ):
919919 for t in self .saturation_texts :
920- t . remove ( )
920+ remove_artist ( t )
921921 self .saturation_texts .clear ()
922922 self .draw_idle ()
923923
@@ -982,7 +982,7 @@ def compute_saturation_and_size(detector_name):
982982
983983 def clear_beam_marker (self ):
984984 while self .beam_marker_artists :
985- self .beam_marker_artists .pop (0 ). remove ( )
985+ remove_artist ( self .beam_marker_artists .pop (0 ))
986986
987987 def update_beam_marker (self ):
988988 self .clear_beam_marker ()
@@ -1767,7 +1767,7 @@ def clear_azimuthal_overlay_artists(self):
17671767 while self .azimuthal_overlay_artists :
17681768 item = self .azimuthal_overlay_artists .pop (0 )
17691769 for artist in item ['artists' ].values ():
1770- artist . remove ( )
1770+ remove_artist ( artist )
17711771
17721772 def save_azimuthal_plot (self ):
17731773 if self .mode != ViewType .polar :
@@ -1836,7 +1836,7 @@ def update_azimuthal_plot_overlays(self):
18361836 self .azimuthal_integral_axis .legend ()
18371837 elif (axis := self .azimuthal_integral_axis ) and axis .get_legend ():
18381838 # Only remove the legend if the axis exists and it has a legend
1839- axis .get_legend (). remove ( )
1839+ remove_artist ( axis .get_legend ())
18401840 self .draw_idle ()
18411841
18421842 def update_azimuthal_integral_plot (self ):
@@ -2269,7 +2269,7 @@ def update_mask_boundaries(self, axis):
22692269
22702270 def clear_mask_boundaries (self ):
22712271 for artist in self ._mask_boundary_artists :
2272- artist . remove ( )
2272+ remove_artist ( artist )
22732273
22742274 self ._mask_boundary_artists .clear ()
22752275
@@ -2550,14 +2550,3 @@ def to_stereo(xys, panel, iviewer):
25502550 raise Exception (f'Unknown mode: { mode } ' )
25512551
25522552 return funcs [mode ]
2553-
2554-
2555- def _safe_remove_artist (artist : Artist ):
2556- # Starting in matplotlib 3.10, we cannot remove artists from a figure
2557- # that has already been cleared. I don't know of any easy way to check
2558- # if the axis has been cleared, though, so for now, we just try to
2559- # remove the artist and ignore the relevant exception if it occurs.
2560- try :
2561- artist .remove ()
2562- except NotImplementedError :
2563- pass
0 commit comments