[AstroPy] matplotlib histograms and astropy quantites

federica fb55 at nyu.edu
Wed Jul 27 08:34:43 EDT 2016


This a numpy version compatibility issue with versions 1.9.x of numpy:

https://github.com/numpy/numpy/issues/6469

and the motivation behind it is computational efficiency, but it is fixed
in later numpy versions

https://github.com/numpy/numpy/commit/34b582aadae8272e7b7209f7a05594e9258ba217


On Wed, Jul 27, 2016 at 5:15 AM, Daniel Evans <d.f.evans at keele.ac.uk> wrote:

> John,
>
> My version of astropy is far too ancient to have this feature (I should
> bug my sysadmin!) so I can't test it myself, but does calling
> astropy.visualization.quantity_support() fix your errors?
>
> "Enable support for plotting astropy.units.Quantity instances in
> matplotlib."
>
> http://docs.astropy.org/en/stable/api/astropy.visualization.quantity_support.html#astropy.visualization.quantity_support
>
> Regards,
> Daniel
>
>
> On 27 July 2016 at 10:00, John K. Parejko <parejkoj at uw.edu> wrote:
>
>> I wonder if I'm misusing astropy quantities, or if there's a bug with how
>> it interacts with matplotlib's pyplot.hist (and thus np.histogram). I've
>> attached a minimal working example that represents a histogram I was
>> attempting to create. If you try to make the plot range be a unit, plt.hist
>> will fail. I would have thought that I could specify a range with units and
>> get the plot axis to match the units I want to show the data with.
>>
>> Also, trying to draw a vertical line with axvline() on a Quantity fails,
>> though I guess I shouldn't be surprised at that. But given the above, this
>> was also surprising to me.
>>
>> I feel like there could be some more documentation about the interaction
>> between astropy quantities and matplotlib?
>>
>> Am I missing something here?
>>
>> Thanks in advance,
>> John
>>
>>
>> import numpy as np
>> from astropy import units as u
>> import matplotlib.pyplot as plt
>>
>> range1 = (-3, 3)
>> range2 = range1*u.arcsecond
>> np.random.seed(100)
>> data = np.random.normal(scale=3e-6, size=100)*u.radian
>> plt.figure()
>> # Not well centered and wrong X scale.
>> plt.hist(data)
>> # Works: I guess I shouldn't be surprised that I have to extract a value
>> here.
>> plt.axvline(x=data[0].value, color='red')
>> plt.figure()
>> # Wanted the range in arcseconds, so this isn't right!
>> plt.hist(data, range=range1)
>> # Dimensionalizing it seems to work correctly.
>> plt.axvline(x=data[0]/u.arcsecond, color='red')
>> plt.figure()
>> # UnitsError: Can only apply 'less_equal' function to dimensionless
>> quantities...
>> plt.hist(data, range=range2)
>> # UnitsError: Can only apply 'greater' function to dimensionless
>> quantities...
>> plt.axvline(x=data[0])
>>
>>
>>
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at scipy.org
>> https://mail.scipy.org/mailman/listinfo/astropy
>>
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy
>
>


-- 

_____________________________________________

dr. federica bianco
NYU-Center for Cosmology & Particle Physics
NYU-Center for Urban Science & Progress
New York, NY

http://cosmo.nyu.edu/~fb55/
___________________________________________

  //\\
 //||\\     Please consider the environment before printing this e-mail

email checking schedule:
9-5 on the hour. otherwise sporadically.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20160727/0d2d9bd9/attachment.html>


More information about the AstroPy mailing list