From 10be04a1ae16fcc3724939c1820452f36d4994ad Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 11 Jan 2022 13:18:20 +0000 Subject: [PATCH 1/2] Fix Bloch sphere with Matplotlib 3.4 --- qiskit/visualization/bloch.py | 2 ++ .../notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml diff --git a/qiskit/visualization/bloch.py b/qiskit/visualization/bloch.py index 97f1c28af14..ac5dd8c3e72 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 00000000000..c4878261d0e --- /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.4. For example, the following code would previously crash with a + message:: + + AttributeError: 'Arrow3D' object has no attribute '_path2d' + + but will now succeed with both Matplotlib 3.4 and 3.5:: + + from qiskit.visualization import plot_bloch_vector + plot_bloch_vector([0, 1, 0]).savefig("tmp.png", bbox_inches='tight') From 807a5101ad299cac6a18c7b78f76ae82b6e9a00f Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 11 Jan 2022 14:01:40 +0000 Subject: [PATCH 2/2] Include Matplotlib 3.3 in release note --- releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml b/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml index c4878261d0e..730d6c7cc0f 100644 --- a/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml +++ b/releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml @@ -3,12 +3,12 @@ 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.4. For example, the following code would previously crash with a - message:: + 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 both Matplotlib 3.4 and 3.5:: + 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')