|
11 | 11 |
|
12 | 12 | import numpy as np
|
13 | 13 |
|
| 14 | +from contextlib import contextmanager |
| 15 | + |
14 | 16 | from matplotlib import (
|
15 | 17 | artist, cbook, colors as mcolors, lines, text as mtext,
|
16 | 18 | path as mpath)
|
@@ -629,10 +631,12 @@ def __init__(self, *args, zs=0, zdir='z', depthshade=True, **kwargs):
|
629 | 631 | self._in_draw = False
|
630 | 632 | super().__init__(*args, **kwargs)
|
631 | 633 | self.set_3d_properties(zs, zdir)
|
| 634 | + self._offset_zordered = None |
632 | 635 |
|
633 | 636 | def draw(self, renderer):
|
634 |
| - with cbook._setattr_cm(self, _in_draw=True): |
635 |
| - super().draw(renderer) |
| 637 | + with self._use_zordered_offset(): |
| 638 | + with cbook._setattr_cm(self, _in_draw=True): |
| 639 | + super().draw(renderer) |
636 | 640 |
|
637 | 641 | def set_sort_zpos(self, val):
|
638 | 642 | """Set the position to use for z-sorting."""
|
@@ -731,15 +735,32 @@ def do_3d_projection(self):
|
731 | 735 | if len(self._linewidths3d) > 1:
|
732 | 736 | self._linewidths = self._linewidths3d[z_markers_idx]
|
733 | 737 |
|
| 738 | + PathCollection.set_offsets(self, np.column_stack((vxs, vys))) |
| 739 | + |
734 | 740 | # Re-order items
|
735 | 741 | vzs = vzs[z_markers_idx]
|
736 | 742 | vxs = vxs[z_markers_idx]
|
737 | 743 | vys = vys[z_markers_idx]
|
738 | 744 |
|
739 |
| - PathCollection.set_offsets(self, np.column_stack((vxs, vys))) |
| 745 | + # Store ordered offset for drawing purpose |
| 746 | + self._offset_zordered = np.column_stack((vxs, vys)) |
740 | 747 |
|
741 | 748 | return np.min(vzs) if vzs.size else np.nan
|
742 | 749 |
|
| 750 | + @contextmanager |
| 751 | + def _use_zordered_offset(self): |
| 752 | + if self._offset_zordered is None: |
| 753 | + # Do nothing |
| 754 | + yield |
| 755 | + else: |
| 756 | + # Swap offset with z-ordered offset |
| 757 | + old_offset = self._offsets |
| 758 | + super().set_offsets(self._offset_zordered) |
| 759 | + try: |
| 760 | + yield |
| 761 | + finally: |
| 762 | + self._offsets = old_offset |
| 763 | + |
743 | 764 | def _maybe_depth_shade_and_sort_colors(self, color_array):
|
744 | 765 | color_array = (
|
745 | 766 | _zalpha(color_array, self._vzs)
|
|
0 commit comments