Matplotlib Contour Plots
Jamie Mitchell
jamiemitchell1604 at gmail.com
Wed Aug 20 06:02:19 EDT 2014
On Tuesday, August 19, 2014 10:21:48 PM UTC+1, pec... at pascolo.net wrote:
> Jamie Mitchell <jamiemitchell1604 at gmail.com> 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
This is great and works very well - thank you!!
More information about the Python-list
mailing list