Skip to content

bar plot fails with units #10619

Closed
Closed

Description

@TD22057

Bug report

ax.bar() fails if the input data has units. This is a fairly new bug as it worked fine at some point in the past. The exception is:

  /home/trd/work/git/matplotlib/build/lib.linux-x86_64-3.6/matplotlib/axes/_base.py:2500: RuntimeWarning: invalid value encountered in double_scalars
    delta = (x1t - x0t) * margin

The issue is that the unit conversion code is being run after a call to np.broadcast_arrays so that the x values are not actually arrays, they're still lists of units. The fix is re-order the code in bar() so that the unit conversion happens first.

Code for reproduction

I'll send in a pull request w/ a fix once I can get the right version of free type to install so I can run existing tests and create a baseline file. This occurs on the current master branch code.

New test case being added to test_units.py which will throw the exception:

@image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
                  savefig_kwarg={'dpi': 60})
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 = [units.Epoch("ET", dt=datetime(2009, 4, 25)),
         units.Epoch("ET", dt=datetime(2009, 4, 26)),
         units.Epoch("ET", dt=datetime(2009, 4, 27))]
    width = [3*day, 2*day, 1*day]

    fig, ax = plt.subplots()
    ax.bar(x, width)

Activity

  1. TD22057 commented on Feb 27, 2018

    @TD22057
    ContributorAuthor
  2. jklymak commented on Feb 27, 2018

    @jklymak
    Member

    Thanks for the potential PR: If you don't need the text for the test, you can call as:

    @image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
                      savefig_kwarg={'dpi': 60}, remove_text=True, style='mpl20')
    

    Also, for new tests, we are trying to use 'mpl20'.

  3. tacaswell commented on Feb 27, 2018

    @tacaswell
    Member

    @TD22057 See https://matplotlib.org/devel/contributing.html?highlight=local_freetype#building-matplotlib-for-image-comparison-tests . I have export MPLLOCALFREETYPE=1 in my bashrc which will take care of downloading and building the correct version of freetype.

  4. added this to the v2.2.0 milestone on Feb 27, 2018
  5. added a commit that references this issue on Feb 27, 2018
    daa4d20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions