Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:e 50': 0.05; 'extent': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'itself.': 0.14; 'compatible,': 0.16; 'folks,': 0.16; 'numpy': 0.16; 'objects.': 0.16; 'pylab': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Problem': 0.16; 'typeerror:': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'array': 0.29; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'decimal': 0.31; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'to:addr :python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'skip:i 50': 0.60; 'skip:n 30': 0.60; 'numbers': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'issues:': 0.84; 'cast': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Problem with numpy 2D Histogram Date: Fri, 20 Jun 2014 11:25:44 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd80d2.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403256365 news.xs4all.nl 2872 [2001:888:2000:d::a6]:35977 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73450 Jamie Mitchell wrote: > Hi folks, > > I'm trying to plot a 2D histogram but I'm having some issues: > from pylab import * > import numpy as np > import netCDF4 > hist,xedges,yedges=np.histogram2d(x,y,bins=10) > extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]] > imshow(hist.T,extent=extent,interpolation='nearest') > colorbar() > show() > > After the first line of code I get: > TypeError: Cannot cast array data from dtype('O') to dtype('float64') > according to the rule 'safe' > > I'm using python2.7, x and y are type 'numpy.ndarray' The error message complains about the dtype, i. e. the type of the elements in the array, not the array itself. Make sure the elements are floating point numbers or something compatible, not arbitrary Python objects. As a baseline the following works from pylab import * import numpy as np x, y = np.random.randn(2, 100) print "x", type(x), x.dtype print "y", type(y), y.dtype hist, xedges, yedges = np.histogram2d(x, y, bins=10) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] imshow(hist.T, extent=extent, interpolation='nearest') colorbar() show() while this doesn't: #... x, y = np.random.randn(2, 100) import decimal y = np.array([decimal.Decimal.from_float(v) for v in y]) #...