Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!diesel.cu.mi.it!.POSTED!not-for-mail From: pecore@pascolo.net Newsgroups: comp.lang.python Subject: Re: Matplotlib Contour Plots Date: Tue, 19 Aug 2014 23:21:48 +0200 Organization: The Sun and the Rain. Lines: 79 Message-ID: <87iolo2n0z.fsf@pascolo.net> References: <53ece976$0$29992$c3e8da3$5496439d@news.astraweb.com> <2f9537da-86b8-441d-a214-00123b8416d1@googlegroups.com> <53ee09cd$0$29994$c3e8da3$5496439d@news.astraweb.com> <53ee2397$0$29966$c3e8da3$5496439d@news.astraweb.com> <5a0df87b-6271-48c5-93ec-39949ab7c192@googlegroups.com> <53f295e2$0$29970$c3e8da3$5496439d@news.astraweb.com> <6bc22f4f-1041-4ad4-9a8f-b0b6ec4bd009@googlegroups.com> NNTP-Posting-Host: ppp-31-8.21-151.libero.it Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: virtdiesel.mng.cu.mi.it 1408483308 26209 151.21.8.31 (19 Aug 2014 21:21:48 GMT) X-Complaints-To: abuse@diesel.cu.mi.it NNTP-Posting-Date: Tue, 19 Aug 2014 21:21:48 +0000 (UTC) User-Agent: Gnus/5.101 (Gnus v5.10.10) XEmacs/21.5-b34 (linux) Cancel-Lock: sha1:3xAk1u4jm1id600s0Huve34eMWg= Xref: csiph.com comp.lang.python:76605 Jamie Mitchell writes: > You were right Christian I wanted a shape (2,150). > > Thank you Rustom and Steven your suggestion has worked. > > Unfortunately the data doesn't plot as I imagined. > > What I would like is: > > X-axis - hs_con_sw > Y-axis - te_con_sw > Z-axis - Frequency > > What I would like is for the Z-axis to contour the frequency or > amount of times that the X-axis data and Y-axis data meet at a > particular point or bin. > > Does anyone know what function or graph could best show this? in my understanding, you have 3 arrays of data that describe 3D data points, and you want to draw a 2D contour plot... in this case you have to interpolate the z-values on a regular grid, that's very easy if you already know what to do ;-) here I assume that data is in a .csv file % cat a.csv 0 ≤ x ≤ 10, 0 ≤ y ≤ 10, z = cos(sqrt((x-5)**2_(y-5)**2)) 1.922065,5.827944,-0.998953 7.582322,0.559370,0.411861 5.001753,3.279957,-0.148694 ... of course my z's are different from yours, but this shouldn't be a real problem --- and here it is my *tested* solution (tested on python 2.7, that is), please feel free to adapt to your needs hth, ciao g % cat contour.py from numpy import loadtxt, linspace from matplotlib.mlab import griddata import matplotlib.pyplot as pl # open 'a.csv', specify the delimiter, specify how many header rows, # slurp the data temp_array = loadtxt(open('a.csv'),delimiter=',',skiprows=1) # the shape of temp_array is (N,3), we want its transpose temp_array = temp_array.transpose() # now the shape is (3,N) and we can do "unpack and assignment: x, y, z = temp_array # now the tricky part, # 1: create two arrays with 101 (arbitrary number) equispaced values # between 0 and 10 --- that is the ranges of data x and data y xi = linspace(0,10,101) yi = linspace(0,10,101) # 2: create, by interpolation, the 2D array that contourf so eagerly # awaited! print griddata.__doc__ zi = griddata(x,y,z, xi,yi) # eventually, lets plot the stuff... # see http://matplotlib.org/examples/pylab_examples/griddata_demo.html # for further details and ideas pl.contour (xi,yi,zi,11,linewidths=1,colors='black') pl.contourf(xi,yi,zi); pl.colorbar() # optional pl.gca().set_aspect('equal', 'box') pl.show() % python contour.py