Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bloch sphere with Matplotlib 3.3 and 3.4 #7517

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions qiskit/visualization/bloch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions releasenotes/notes/fix-path2d-mpl3.4-b1af3a23b408d30a.yaml
Original file line number Diff line number Diff line change
@@ -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')