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

Skip to content

Commit daa4d20

Browse files
committed
Fixes #10619. Reordered bar plot to correctly handle units
1 parent 6874c42 commit daa4d20

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,10 +2190,6 @@ 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:
@@ -2211,18 +2207,6 @@ def bar(self, *args, **kwargs):
22112207
else:
22122208
raise ValueError('invalid orientation: %s' % orientation)
22132209

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-
22262210
# lets do some conversions now since some types cannot be
22272211
# subtracted uniformly
22282212
if self.xaxis is not None:
@@ -2237,6 +2221,22 @@ def bar(self, *args, **kwargs):
22372221
if yerr is not None:
22382222
yerr = self.convert_yunits(yerr)
22392223

2224+
x, height, width, y, linewidth = np.broadcast_arrays(
2225+
# Make args iterable too.
2226+
np.atleast_1d(x), height, width, y, linewidth)
2227+
2228+
linewidth = itertools.cycle(np.atleast_1d(linewidth))
2229+
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
2230+
# Fallback if color == "none".
2231+
itertools.repeat([0, 0, 0, 0]))
2232+
if edgecolor is None:
2233+
edgecolor = itertools.repeat(None)
2234+
else:
2235+
edgecolor = itertools.chain(
2236+
itertools.cycle(mcolors.to_rgba_array(edgecolor)),
2237+
# Fallback if edgecolor == "none".
2238+
itertools.repeat([0, 0, 0, 0]))
2239+
22402240
# We will now resolve the alignment and really have
22412241
# left, bottom, width, height vectors
22422242
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: 38 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,40 @@ def test_plot_masked_units():
9596

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

0 commit comments

Comments
 (0)