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

Skip to content

Commit 7a73864

Browse files
authored
Merge pull request #10623 from TD22057/jpl_unit_tests
Fix for failing bar plot w/ units
2 parents d2e68ff + 0a63038 commit 7a73864

File tree

6 files changed

+61
-23
lines changed

6 files changed

+61
-23
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,39 +2190,17 @@ def bar(self, *args, **kwargs):
21902190
adjust_xlim = True
21912191
x = 0
21922192

2193-
x, height, width, y, linewidth = np.broadcast_arrays(
2194-
# Make args iterable too.
2195-
np.atleast_1d(x), height, width, y, linewidth)
2196-
21972193
if orientation == 'vertical':
21982194
self._process_unit_info(xdata=x, ydata=height, kwargs=kwargs)
21992195
if log:
22002196
self.set_yscale('log', nonposy='clip')
2201-
2202-
tick_label_axis = self.xaxis
2203-
tick_label_position = x
22042197
elif orientation == 'horizontal':
22052198
self._process_unit_info(xdata=width, ydata=y, kwargs=kwargs)
22062199
if log:
22072200
self.set_xscale('log', nonposx='clip')
2208-
2209-
tick_label_axis = self.yaxis
2210-
tick_label_position = y
22112201
else:
22122202
raise ValueError('invalid orientation: %s' % orientation)
22132203

2214-
linewidth = itertools.cycle(np.atleast_1d(linewidth))
2215-
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
2216-
# Fallback if color == "none".
2217-
itertools.repeat([0, 0, 0, 0]))
2218-
if edgecolor is None:
2219-
edgecolor = itertools.repeat(None)
2220-
else:
2221-
edgecolor = itertools.chain(
2222-
itertools.cycle(mcolors.to_rgba_array(edgecolor)),
2223-
# Fallback if edgecolor == "none".
2224-
itertools.repeat([0, 0, 0, 0]))
2225-
22262204
# lets do some conversions now since some types cannot be
22272205
# subtracted uniformly
22282206
if self.xaxis is not None:
@@ -2237,6 +2215,30 @@ def bar(self, *args, **kwargs):
22372215
if yerr is not None:
22382216
yerr = self.convert_yunits(yerr)
22392217

2218+
x, height, width, y, linewidth = np.broadcast_arrays(
2219+
# Make args iterable too.
2220+
np.atleast_1d(x), height, width, y, linewidth)
2221+
2222+
# Now that units have been converted, set the tick locations.
2223+
if orientation == 'vertical':
2224+
tick_label_axis = self.xaxis
2225+
tick_label_position = x
2226+
elif orientation == 'horizontal':
2227+
tick_label_axis = self.yaxis
2228+
tick_label_position = y
2229+
2230+
linewidth = itertools.cycle(np.atleast_1d(linewidth))
2231+
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
2232+
# Fallback if color == "none".
2233+
itertools.repeat([0, 0, 0, 0]))
2234+
if edgecolor is None:
2235+
edgecolor = itertools.repeat(None)
2236+
else:
2237+
edgecolor = itertools.chain(
2238+
itertools.cycle(mcolors.to_rgba_array(edgecolor)),
2239+
# Fallback if edgecolor == "none".
2240+
itertools.repeat([0, 0, 0, 0]))
2241+
22402242
# We will now resolve the alignment and really have
22412243
# left, bottom, width, height vectors
22422244
if align == 'center':

lib/matplotlib/testing/jpl_units/EpochConverter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def duration2float( value ):
101101
= RETURN VALUE
102102
- Returns the value parameter converted to floats.
103103
"""
104-
return value.days()
104+
return value.seconds() / 86400.0
105105

106106
#------------------------------------------------------------------------
107107
@staticmethod

lib/matplotlib/testing/jpl_units/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def register():
6565

6666
mplU.registry[ str ] = StrConverter()
6767
mplU.registry[ Epoch ] = EpochConverter()
68+
mplU.registry[ Duration ] = EpochConverter()
6869
mplU.registry[ UnitDbl ] = UnitDblConverter()
6970

7071
#=======================================================================

lib/matplotlib/tests/test_units.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from matplotlib.testing.decorators import image_comparison
44
import matplotlib.units as munits
55
import numpy as np
6+
import datetime
67

78
try:
89
# mock in python 3.3+
@@ -95,3 +96,37 @@ def test_plot_masked_units():
9596

9697
fig, ax = plt.subplots()
9798
ax.plot(data_masked_units)
99+
100+
101+
@image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
102+
savefig_kwarg={'dpi': 120}, style='mpl20')
103+
def test_jpl_bar_units():
104+
from datetime import datetime
105+
import matplotlib.testing.jpl_units as units
106+
units.register()
107+
108+
day = units.Duration("ET", 24.0 * 60.0 * 60.0)
109+
x = [0*units.km, 1*units.km, 2*units.km]
110+
w = [1*day, 2*day, 3*day]
111+
b = units.Epoch("ET", dt=datetime(2009, 4, 25))
112+
113+
fig, ax = plt.subplots()
114+
ax.bar(x, w, bottom=b)
115+
ax.set_ylim([b-1*day, b+w[-1]+1*day])
116+
117+
118+
@image_comparison(baseline_images=['jpl_barh_units'], extensions=['png'],
119+
savefig_kwarg={'dpi': 120}, style='mpl20')
120+
def test_jpl_barh_units():
121+
from datetime import datetime
122+
import matplotlib.testing.jpl_units as units
123+
units.register()
124+
125+
day = units.Duration("ET", 24.0 * 60.0 * 60.0)
126+
x = [0*units.km, 1*units.km, 2*units.km]
127+
w = [1*day, 2*day, 3*day]
128+
b = units.Epoch("ET", dt=datetime(2009, 4, 25))
129+
130+
fig, ax = plt.subplots()
131+
ax.barh(x, w, left=b)
132+
ax.set_xlim([b-1*day, b+w[-1]+1*day])

0 commit comments

Comments
 (0)