Skip to content

Commit 980b63e

Browse files
committed
uses axes in connection patch to convert points
Modifies string convertor to return a scaler when value is only one value, as with datetime
1 parent 9022e41 commit 980b63e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-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 *= fig.dpi / 72
4600+
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_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)