Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 915f672

Browse files
authored
Merge pull request #29167 from story645/connectionpatch_units
BUGFIX: use axes unit information in ConnectionPatch
2 parents 6119c12 + 227180c commit 915f672

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

lib/matplotlib/category.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import numpy as np
1919

20-
from matplotlib import _api, ticker, units
20+
from matplotlib import _api, cbook, ticker, units
2121

2222

2323
_log = logging.getLogger(__name__)
@@ -55,7 +55,8 @@ def convert(value, unit, axis):
5555
values = np.atleast_1d(np.array(value, dtype=object))
5656
# force an update so it also does type checking
5757
unit.update(values)
58-
return np.vectorize(unit._mapping.__getitem__, otypes=[float])(values)
58+
s = np.vectorize(unit._mapping.__getitem__, otypes=[float])(values)
59+
return s if not cbook.is_scalar_or_string(value) else s[0]
5960

6061
@staticmethod
6162
def axisinfo(unit, axis):

lib/matplotlib/patches.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4589,23 +4589,27 @@ def _get_xy(self, xy, s, axes=None):
45894589
s0 = s # For the error message, if needed.
45904590
if axes is None:
45914591
axes = self.axes
4592-
xy = np.array(xy)
4592+
4593+
# preserve mixed type input (such as str, int)
4594+
x = np.array(xy[0])
4595+
y = np.array(xy[1])
4596+
45934597
fig = self.get_figure(root=False)
45944598
if s in ["figure points", "axes points"]:
4595-
xy *= fig.dpi / 72
4599+
x = x * fig.dpi / 72
4600+
y = y * fig.dpi / 72
45964601
s = s.replace("points", "pixels")
45974602
elif s == "figure fraction":
45984603
s = fig.transFigure
45994604
elif s == "subfigure fraction":
46004605
s = fig.transSubfigure
46014606
elif s == "axes fraction":
46024607
s = axes.transAxes
4603-
x, y = xy
46044608

46054609
if s == 'data':
46064610
trans = axes.transData
4607-
x = float(self.convert_xunits(x))
4608-
y = float(self.convert_yunits(y))
4611+
x = cbook._to_unmasked_float_array(axes.xaxis.convert_units(x))
4612+
y = cbook._to_unmasked_float_array(axes.yaxis.convert_units(y))
46094613
return trans.transform((x, y))
46104614
elif s == 'offset points':
46114615
if self.xycoords == 'offset points': # prevent recursion

lib/matplotlib/tests/test_patches.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,28 @@ def test_connection_patch_fig(fig_test, fig_ref):
606606
fig_ref.add_artist(con)
607607

608608

609+
@check_figures_equal(extensions=["png"])
610+
def test_connection_patch_pixel_points(fig_test, fig_ref):
611+
xyA_pts = (.3, .2)
612+
xyB_pts = (-30, -20)
613+
614+
ax1, ax2 = fig_test.subplots(1, 2)
615+
con = mpatches.ConnectionPatch(xyA=xyA_pts, coordsA="axes points", axesA=ax1,
616+
xyB=xyB_pts, coordsB="figure points",
617+
arrowstyle="->", shrinkB=5)
618+
fig_test.add_artist(con)
619+
620+
plt.rcParams["savefig.dpi"] = plt.rcParams["figure.dpi"]
621+
622+
ax1, ax2 = fig_ref.subplots(1, 2)
623+
xyA_pix = (xyA_pts[0]*(fig_ref.dpi/72), xyA_pts[1]*(fig_ref.dpi/72))
624+
xyB_pix = (xyB_pts[0]*(fig_ref.dpi/72), xyB_pts[1]*(fig_ref.dpi/72))
625+
con = mpatches.ConnectionPatch(xyA=xyA_pix, coordsA="axes pixels", axesA=ax1,
626+
xyB=xyB_pix, coordsB="figure pixels",
627+
arrowstyle="->", shrinkB=5)
628+
fig_ref.add_artist(con)
629+
630+
609631
def test_datetime_rectangle():
610632
# Check that creating a rectangle with timedeltas doesn't fail
611633
from datetime import datetime, timedelta

lib/matplotlib/tests/test_units.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import matplotlib.pyplot as plt
66
from matplotlib.testing.decorators import check_figures_equal, image_comparison
7+
import matplotlib.patches as mpatches
78
import matplotlib.units as munits
89
from matplotlib.category import StrCategoryConverter, UnitData
910
from matplotlib.dates import DateConverter
@@ -336,3 +337,17 @@ def test_plot_kernel():
336337
# just a smoketest that fail
337338
kernel = Kernel([1, 2, 3, 4, 5])
338339
plt.plot(kernel)
340+
341+
342+
def test_connection_patch_units(pd):
343+
# tests that this doesn't raise an error
344+
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(10, 5))
345+
x = pd.Timestamp('2017-01-01T12')
346+
ax1.axvline(x)
347+
y = "test test"
348+
ax2.axhline(y)
349+
arr = mpatches.ConnectionPatch((x, 0), (0, y),
350+
coordsA='data', coordsB='data',
351+
axesA=ax1, axesB=ax2)
352+
fig.add_artist(arr)
353+
fig.draw_without_rendering()

0 commit comments

Comments
 (0)