Skip to content

Fix for failing bar plot w/ units #10623

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 4 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
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
46 changes: 24 additions & 22 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,39 +2190,17 @@ def bar(self, *args, **kwargs):
adjust_xlim = True
x = 0

x, height, width, y, linewidth = np.broadcast_arrays(
# Make args iterable too.
np.atleast_1d(x), height, width, y, linewidth)

if orientation == 'vertical':
self._process_unit_info(xdata=x, ydata=height, kwargs=kwargs)
if log:
self.set_yscale('log', nonposy='clip')

tick_label_axis = self.xaxis
tick_label_position = x
elif orientation == 'horizontal':
self._process_unit_info(xdata=width, ydata=y, kwargs=kwargs)
if log:
self.set_xscale('log', nonposx='clip')

tick_label_axis = self.yaxis
tick_label_position = y
else:
raise ValueError('invalid orientation: %s' % orientation)

linewidth = itertools.cycle(np.atleast_1d(linewidth))
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
# Fallback if color == "none".
itertools.repeat([0, 0, 0, 0]))
if edgecolor is None:
edgecolor = itertools.repeat(None)
else:
edgecolor = itertools.chain(
itertools.cycle(mcolors.to_rgba_array(edgecolor)),
# Fallback if edgecolor == "none".
itertools.repeat([0, 0, 0, 0]))

# lets do some conversions now since some types cannot be
# subtracted uniformly
if self.xaxis is not None:
Expand All @@ -2237,6 +2215,30 @@ def bar(self, *args, **kwargs):
if yerr is not None:
yerr = self.convert_yunits(yerr)

x, height, width, y, linewidth = np.broadcast_arrays(
# Make args iterable too.
np.atleast_1d(x), height, width, y, linewidth)

# Now that units have been converted, set the tick locations.
if orientation == 'vertical':
tick_label_axis = self.xaxis
tick_label_position = x
elif orientation == 'horizontal':
tick_label_axis = self.yaxis
tick_label_position = y

linewidth = itertools.cycle(np.atleast_1d(linewidth))
color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
# Fallback if color == "none".
itertools.repeat([0, 0, 0, 0]))
if edgecolor is None:
edgecolor = itertools.repeat(None)
else:
edgecolor = itertools.chain(
itertools.cycle(mcolors.to_rgba_array(edgecolor)),
# Fallback if edgecolor == "none".
itertools.repeat([0, 0, 0, 0]))

# We will now resolve the alignment and really have
# left, bottom, width, height vectors
if align == 'center':
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/testing/jpl_units/EpochConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def duration2float( value ):
= RETURN VALUE
- Returns the value parameter converted to floats.
"""
return value.days()
return value.seconds() / 86400.0

#------------------------------------------------------------------------
@staticmethod
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/testing/jpl_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def register():

mplU.registry[ str ] = StrConverter()
mplU.registry[ Epoch ] = EpochConverter()
mplU.registry[ Duration ] = EpochConverter()
mplU.registry[ UnitDbl ] = UnitDblConverter()

#=======================================================================
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from matplotlib.testing.decorators import image_comparison
import matplotlib.units as munits
import numpy as np
import datetime

try:
# mock in python 3.3+
Expand Down Expand Up @@ -95,3 +96,37 @@ def test_plot_masked_units():

fig, ax = plt.subplots()
ax.plot(data_masked_units)


@image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
savefig_kwarg={'dpi': 120}, style='mpl20')
def test_jpl_bar_units():
from datetime import datetime
import matplotlib.testing.jpl_units as units
units.register()

day = units.Duration("ET", 24.0 * 60.0 * 60.0)
x = [0*units.km, 1*units.km, 2*units.km]
w = [1*day, 2*day, 3*day]
b = units.Epoch("ET", dt=datetime(2009, 4, 25))

fig, ax = plt.subplots()
ax.bar(x, w, bottom=b)
ax.set_ylim([b-1*day, b+w[-1]+1*day])


@image_comparison(baseline_images=['jpl_barh_units'], extensions=['png'],
savefig_kwarg={'dpi': 120}, style='mpl20')
def test_jpl_barh_units():
from datetime import datetime
import matplotlib.testing.jpl_units as units
units.register()

day = units.Duration("ET", 24.0 * 60.0 * 60.0)
x = [0*units.km, 1*units.km, 2*units.km]
w = [1*day, 2*day, 3*day]
b = units.Epoch("ET", dt=datetime(2009, 4, 25))

fig, ax = plt.subplots()
ax.barh(x, w, left=b)
ax.set_xlim([b-1*day, b+w[-1]+1*day])