8000 Replace quiver dpi callback with reinit-on-dpi-changed. by anntzer · Pull Request #22807 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Replace quiver dpi callback with reinit-on-dpi-changed. #22807

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
merged 1 commit into from
Apr 9, 2022
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

import numpy as np
from numpy import ma
Expand Down Expand Up @@ -299,21 +298,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 @@ -329,18 +313,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
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why this condition is here if its always true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As above.

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 @@ -363,7 +345,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 @@ -534,26 +516,7 @@ def __init__(self, ax, *args,
closed=False, **kwargs)
self.polykw = kwargs
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 @@ -562,18 +525,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._initialized:
if True: # self._dpi_at_last_init != self.axes.figure.dpi
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change the comment? why does this code get run unconditionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why the original check got commented out (and don't really want to investigate right now), I'm just replacing _initialized by its newer moral equivalent.

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 @@ -589,7 +553,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 54AF Expand Up @@ -618,7 +581,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