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 compatibility with Matplotlib 3.5 #7301

Merged
merged 3 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ stages:
python -m pip install --upgrade pip
pip install -U -r requirements.txt -r requirements-dev.txt -c constraints.txt
pip install -c constraints.txt -e .
pip install "qiskit-ibmq-provider" "qiskit-aer" "z3-solver" "qiskit-ignis" "matplotlib>=3.3.0,<3.5" sphinx nbsphinx sphinx_rtd_theme cvxpy -c constraints.txt
pip install "qiskit-ibmq-provider" "qiskit-aer" "z3-solver" "qiskit-ignis" "matplotlib>=3.3.0" sphinx nbsphinx sphinx_rtd_theme cvxpy -c constraints.txt
python setup.py build_ext --inplace
sudo apt install -y graphviz pandoc
pip check
Expand Down
27 changes: 22 additions & 5 deletions qiskit/visualization/bloch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,36 @@
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import Axes3D, proj3d
from mpl_toolkits.mplot3d.art3d import Patch3D

from .utils import matplotlib_close_if_inline


class Arrow3D(FancyArrowPatch):
class Arrow3D(Patch3D, FancyArrowPatch):
"""Makes a fancy arrow"""

def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
# Nasty hack around a poorly implemented deprecation warning in Matplotlib 3.5 that issues two
# deprecation warnings if an artist's module does not claim to be part of the below module.
# This revolves around the method `Patch3D.do_3d_projection(self, renderer=None)`. The
# `renderer` argument has been deprecated since Matplotlib 3.4, but in 3.5 some internal calls
# during `Axes3D` display started calling the method. If an artist does not have this module,
# then it issues a deprecation warning, and calls it by passing the `renderer` parameter as
# well, which consequently triggers another deprecation warning. We should be able to remove
# this once 3.6 is the minimum supported version, because the deprecation period ends then.
Comment on lines +71 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, it'll be a while before we can make our matplotlib requirement >=3.6.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I know - here it's just that I don't like putting in gross hacks without saying the condition that they can be removed again!

__module__ = "mpl_toolkits.mplot3d.art3d"

def __init__(self, xs, ys, zs, zdir="z", **kwargs):
# The Patch3D.__init__() method just calls its own super() method and then
# self.set_3d_properties, but it its __init__ signature is actually pretty incompatible with
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
# how it goes on to call set_3d_properties, so we just have to do things ourselves. The
# parent of Patch3D is Patch, which is also a parent of FancyArrowPatch, so its __init__ is
# still getting suitably called.
# pylint: disable=super-init-not-called
FancyArrowPatch.__init__(self, (0, 0), (0, 0), **kwargs)
self.set_3d_properties(tuple(zip(xs, ys)), zs, zdir)

def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs3d, ys3d, zs3d = zip(*self._segment3d)
x_s, y_s, _ = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
self.set_positions((x_s[0], y_s[0]), (x_s[1], y_s[1]))
FancyArrowPatch.draw(self, renderer)
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-matplotlib-3.5-40f6d1a109ae06fe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fixes:
- |
Fixed a compatibility issue with Matplotlib 3.5, where the Bloch sphere would fail to render if it had any vectors attached, such as by using :obj:`~qiskit.visualization.plot_bloch_vector`.
See `#7272 <https://github.com/Qiskit/qiskit-terra/issues/7272>`__ for more detail.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ipython<7.22.0
ipykernel<5.5.2
ipywidgets>=7.3.0
jupyter
matplotlib>=3.3,<3.5
matplotlib>=3.3
pillow>=4.2.1
black==21.4b2
pydot
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@


visualization_extras = [
"matplotlib>=3.3,<3.5",
"matplotlib>=3.3",
"ipywidgets>=7.3.0",
"pydot",
"pillow>=4.2.1",
Expand Down