@@ -516,7 +516,7 @@ def _draw_panel_borders(self):
516516 clip_path = ax .patch ,
517517 clip_on = False ,
518518 )
519- self .figure . add_artist (rect )
519+ self ._add_figure_artist (rect )
520520 self .theme .targets .panel_border .append (rect )
521521
522522 def _draw_layers (self ):
@@ -561,43 +561,39 @@ def _draw_figure_texts(self):
561561 """
562562 Draw title, x label, y label and caption onto the figure
563563 """
564- figure = self .figure
565- theme = self .theme
566- targets = theme .targets
564+ from matplotlib .text import Text
567565
568- title = self .labels .get ("title" , "" )
569- subtitle = self .labels .get ("subtitle" , "" )
570- caption = self .labels .get ("caption" , "" )
571- tag = self .labels .get ("tag" , "" )
572- footer = self .labels .get ("footer" , "" )
573-
574- # Get the axis labels (default or specified by user)
575- # and let the coordinate modify them e.g. flip
576- labels = self .coordinates .labels (
577- self .layout .set_xy_labels (self .labels )
578- )
566+ targets = self .theme .targets
579567
580568 # The locations are handled by the layout manager
581- if title :
582- targets .plot_title = figure . text ( 0 , 0 , title )
569+ if title := self . labels . get ( "title" , "" ) :
570+ targets .plot_title = self . _add_figure_artist ( Text ( text = title ) )
583571
584- if subtitle :
585- targets .plot_subtitle = figure .text (0 , 0 , subtitle )
572+ if subtitle := self .labels .get ("subtitle" , "" ):
573+ targets .plot_subtitle = self ._add_figure_artist (
574+ Text (text = subtitle )
575+ )
576+
577+ if caption := self .labels .get ("caption" , "" ):
578+ targets .plot_caption = self ._add_figure_artist (Text (text = caption ))
586579
587- if caption :
588- targets .plot_caption = figure . text ( 0 , 0 , caption )
580+ if footer := self . labels . get ( "footer" , "" ) :
581+ targets .plot_footer = self . _add_figure_artist ( Text ( text = footer ) )
589582
590- if footer :
591- targets .plot_footer = figure . text ( 0 , 0 , footer )
583+ if tag := self . labels . get ( "tag" , "" ) :
584+ targets .plot_tag = self . _add_figure_artist ( Text ( text = tag ) )
592585
593- if tag :
594- targets .plot_tag = figure .text (0 , 0 , tag )
586+ # Get the axis labels (default or specified by user)
587+ # and let the coordinate modify them e.g. flip
588+ labels = self .coordinates .labels (
589+ self .layout .set_xy_labels (self .labels )
590+ )
595591
596592 if labels .x :
597- targets .axis_title_x = figure . text ( 0 , 0 , labels .x )
593+ targets .axis_title_x = self . _add_figure_artist ( Text ( text = labels .x ) )
598594
599595 if labels .y :
600- targets .axis_title_y = figure . text ( 0 , 0 , labels .y )
596+ targets .axis_title_y = self . _add_figure_artist ( Text ( text = labels .y ) )
601597
602598 def _draw_watermarks (self ):
603599 """
@@ -611,39 +607,60 @@ def _draw_insets(self):
611607 Draw every inset attached to this plot into the host figure
612608
613609 Each inset reuses the host's figure instead of creating its own.
614- The inset's zorder is raised above the host's so its axes paint
615- on top; for Compose insets this zorder is propagated down the
616- sub-tree when the composition is drawn.
610+ The inset's zorder is raised by `INSET_ZORDER_STEP` so every
611+ figure-level artist on the inset sits above the host's; for
612+ Compose insets this zorder is propagated down the sub-tree when
613+ the composition is drawn.
617614 """
615+ from .composition ._inset_element import INSET_ZORDER_STEP
616+
618617 for inset in self ._insets :
619618 inset .obj .figure = self .figure
620- inset .obj ._zorder = self ._zorder + 1
619+ inset .obj ._zorder = self ._zorder + INSET_ZORDER_STEP
621620 inset .obj .draw ()
622621
622+ def _add_figure_artist (self , artist ):
623+ """
624+ Add an artist to this plot's figure with the right zorder offset
625+
626+ For a top-level plot this is a no-op offset; on an inset every
627+ figure-level artist (titles, panel borders, legends, strip text,
628+ ...) is shifted up by the inset's `_zorder` so the inset sits
629+ wholly above the host. Returns the artist so the call site can
630+ keep a single-statement assignment.
631+ """
632+ artist .set_zorder (artist .get_zorder () + self ._zorder )
633+ self .figure .add_artist (artist )
634+ return artist
635+
623636 def _draw_plot_background (self ):
624637 from matplotlib .lines import Line2D
625638 from matplotlib .patches import Rectangle
626639
627- zorder = - 1000
628- rect = Rectangle ((0 , 0 ), 0 , 0 , facecolor = "none" , zorder = zorder )
629- self .figure .add_artist (rect )
630- self ._gridspec .patch = rect
631- self .theme .targets .plot_background = rect
640+ targets = self .theme .targets
641+
642+ # The background sits just below this plot's own axes layer.
643+ # _add_figure_artist offsets every figure-level artist by
644+ # self._zorder, so for a top-level plot this stays at -0.5
645+ # and for an inset it sits between the host's axes and the
646+ # inset's axes — the inset background covers the host.
647+ targets .plot_background = self ._add_figure_artist (
648+ Rectangle ((0 , 0 ), 0 , 0 , facecolor = "none" , zorder = - 0.5 )
649+ )
650+ self ._gridspec .patch = targets .plot_background
632651
633652 # Footer background and line only if there is a footer, and put
634653 # it on top of the plot background
635654 if self .labels .get ("footer" , "" ):
636- rect = Rectangle (
637- (0 , 0 ), 0 , 0 , facecolor = "none" , linewidth = 0 , zorder = zorder + 1
655+ targets .plot_footer_background = self ._add_figure_artist (
656+ Rectangle (
657+ (0 , 0 ), 0 , 0 , facecolor = "none" , linewidth = 0 , zorder = - 0.4
658+ )
638659 )
639- self .figure .add_artist (rect )
640- self .theme .targets .plot_footer_background = rect
641660
642- line = Line2D (
643- [0 , 0 ], [0 , 0 ], color = "none" , linewidth = 0 , zorder = zorder + 2
661+ targets . plot_footer_line = self . _add_figure_artist (
662+ Line2D ( [0 , 0 ], [0 , 0 ], color = "none" , linewidth = 0 , zorder = - 0.3 )
644663 )
645- self .figure .add_artist (line )
646- self .theme .targets .plot_footer_line = line
647664
648665 def _save_filename (self , ext : str ) -> Path :
649666 """
0 commit comments