8000 Backport PR #22807 on branch v3.5.x (Replace quiver dpi callback with reinit-on-dpi-changed.) by meeseeksmachine · Pull Request #22837 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #22807 on branch v3.5.x (Replace quiver dpi callback with reinit-on-dpi-changed.) #22837

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

Merged
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
62 changes: 12 additions & 50 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""

import math
import weakref

impor 8000 t numpy as np
from numpy import ma
Expand Down Expand Up @@ -273,21 +272,6 @@ def __init__(self, Q, X, Y, U, label,
self.color = color
self.label = label
self._labelsep_inches = labelsep
self.labelsep = (self._labelsep_inches * Q.axes.figure.dpi)

# try to prevent closure over the real self
weak_self = weakref.ref(self)

def on_dpi_change(fig):
self_weakref = weak_self()
if self_weakref is not None:
self_weakref.labelsep = self_weakref._labelsep_inches * fig.dpi
# simple brute force update works because _init is called at
# the start of draw.
self_weakref._initialized = False

self._cid = Q.axes.figure.callbacks.connect(
'dpi_changed', on_dpi_change)

self.labelpos = labelpos
self.labelcolor = labelcolor
Expand All @@ -303,18 +287,16 @@ def on_dpi_change(fig):

if self.labelcolor is not None:
self.text.set_color(self.labelcolor)
self._initialized = False
self._dpi_at_last_init = None
self.zorder = Q.zorder + 0.1

def remove(self):
# docstring inherited
self.Q.axes.figure.callbacks.disconnect(self._cid)
self._cid = None
super().remove() # pass the remove call up the stack
@property
def labelsep(self):
return self._labelsep_inches * self.Q.axes.figure.dpi

def _init(self):
if True: # not self._initialized:
if not self.Q._initialized:
if True: # self._dpi_at_last_init != self.axes.figure.dpi
if self.Q._dpi_at_last_init != self.Q.axes.figure.dpi:
self.Q._init()
self._set_transform()
with cbook._setattr_cm(self.Q, pivot=self.pivot[self.labelpos],
Expand All @@ -337,7 +319,7 @@ def _init(self):
self.vector.set_color(self.color)
self.vector.set_transform(self.Q.get_transform())
self.vector.set_figure(self.get_figure())
self._initialized = True
self._dpi_at_last_init = self.Q.axes.figure.dpi

def _text_x(self, x):
if self.labelpos == 'E':
Expand Down Expand Up @@ -508,26 +490,7 @@ def __init__(self, ax, *args,
closed=False, **kw)
self.polykw = kw
self.set_UVC(U, V, C)
self._initialized = False

weak_self = weakref.ref(self) # Prevent closure over the real self.

def on_dpi_change(fig):
self_weakref = weak_self()
if self_weakref is not None:
# vertices depend on width, span which in turn depend on dpi
self_weakref._new_UV = True
# simple brute force update works because _init is called at
# the start of draw.
self_weakref._initialized = False

self._cid = ax.figure.callbacks.connect('dpi_changed', on_dpi_change)

def remove(self):
# docstring inherited
self.axes.figure.callbacks.disconnect(self._cid)
self._cid = None
super().remove() # pass the remove call up the stack
self._dpi_at_last_init = None

def _init(self):
"""
Expand All @@ -536,18 +499,19 @@ def _init(self):
"""
# It seems that there are not enough event notifications
# available to have this work on an as-needed basis at present.
if True: # not self._initia A831 lized:
if True: # self._dpi_at_last_init != self.axes.figure.dpi
trans = self._set_transform()
self.span = trans.inverted().transform_bbox(self.axes.bbox).width
if self.width is None:
sn = np.clip(math.sqrt(self.N), 8, 25)
self.width = 0.06 * self.span / sn

# _make_verts sets self.scale if not already specified
if not self._initialized and self.scale is None:
if (self._dpi_at_last_init != self.axes.figure.dpi
and self.scale is None):
self._make_verts(self.U, self.V, self.angles)

self._initialized = True
self._dpi_at_last_init = self.axes.figure.dpi

def get_datalim(self, transData):
trans = self.get_transform()
Expand All @@ -563,7 +527,6 @@ def draw(self, renderer):
self._init()
verts = self._make_verts(self.U, self.V, self.angles)
self.set_verts(verts, closed=False)
self._new_UV = False
super().draw(renderer)
self.stale = False

Expand Down Expand Up @@ -592,7 +555,6 @@ def set_UVC(self, U, V, C=None):
self.Umask = mask
if C is not None:
self.set_array(C)
self._new_UV = True
self.stale = True

def _dots_per_unit(self, units):
Expand Down
0