diff --git a/qiskit/visualization/bloch.py b/qiskit/visualization/bloch.py index 97f1c28af148..ac5dd8c3e720 100644 --- a/qiskit/visualization/bloch.py +++ b/qiskit/visualization/bloch.py @@ -81,10 +81,12 @@ def __init__(self, xs, ys, zs, zdir="z", **kwargs): # pylint: disable=super-init-not-called FancyArrowPatch.__init__(self, (0, 0), (0, 0), **kwargs) self.set_3d_properties(tuple(zip(xs, ys)), zs, zdir) + self._path2d = None def draw(self, renderer): xs3d, ys3d, zs3d = zip(*self._segment3d) x_s, y_s, _ = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M) + self._path2d = matplotlib.path.Path(np.column_stack([x_s, y_s])) self.set_positions((x_s[0], y_s[0]), (x_s[1], y_s[1])) FancyArrowPatch.draw(self, renderer) diff --git a/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml b/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml new file mode 100644 index 000000000000..730d6c7cc0f9 --- /dev/null +++ b/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml @@ -0,0 +1,14 @@ +--- +fixes: + - | + Fixed an issue where plotting Bloch spheres could cause an ``AttributeError`` + to be raised in Jupyter or when trying to crop figures down to size with + Matplotlib 3.3 or 3.4 (but not 3.5). For example, the following code would + previously crash with a message:: + + AttributeError: 'Arrow3D' object has no attribute '_path2d' + + but will now succeed with all current supported versions of Matplotlib:: + + from qiskit.visualization import plot_bloch_vector + plot_bloch_vector([0, 1, 0]).savefig("tmp.png", bbox_inches='tight')