Skip to content

bar plot fails with units #10619

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

Closed
TD22057 opened this issue Feb 27, 2018 · 3 comments · Fixed by #10623
Closed

bar plot fails with units #10619

TD22057 opened this issue Feb 27, 2018 · 3 comments · Fixed by #10623

Comments

@TD22057
Copy link
Contributor

TD22057 commented Feb 27, 2018

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)
@TD22057
Copy link
Contributor Author

TD22057 commented Feb 27, 2018

FYI: @jrevans

@jklymak
Copy link
Member

jklymak commented Feb 27, 2018

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'.

@tacaswell
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants