Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76315 > unrolled thread
| Started by | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| First post | 2014-08-14 08:22 -0700 |
| Last post | 2014-08-18 13:37 +0300 |
| Articles | 17 — 8 participants |
Back to article view | Back to comp.lang.python
Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-14 08:22 -0700
Re: Matplotlib Contour Plots Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-15 02:53 +1000
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-15 04:54 -0700
Re: Matplotlib Contour Plots Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-15 23:23 +1000
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-15 07:42 -0700
Re: Matplotlib Contour Plots Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-16 01:13 +1000
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-18 09:51 -0700
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-18 09:55 -0700
Re: Matplotlib Contour Plots Rustom Mody <rustompmody@gmail.com> - 2014-08-18 10:16 -0700
Re: Matplotlib Contour Plots Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-19 10:10 +1000
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-19 01:43 -0700
Re: Matplotlib Contour Plots Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-08-19 09:12 -0400
Re: Matplotlib Contour Plots pecore@pascolo.net - 2014-08-19 23:21 +0200
Re: Matplotlib Contour Plots Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-08-20 03:02 -0700
Re: Matplotlib Contour Plots Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-20 17:55 +0100
Re: Matplotlib Contour Plots Christian Gollwitzer <auriocus@gmx.de> - 2014-08-18 19:49 +0200
Re: Matplotlib Contour Plots Anssi Saari <as@sci.fi> - 2014-08-18 13:37 +0300
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-14 08:22 -0700 |
| Subject | Matplotlib Contour Plots |
| Message-ID | <e1bbdda8-47a8-4849-8180-1f1a08aeb059@googlegroups.com> |
Hello all, I want to contour a scatter plot but I don't know how. Can anyone help me out? Cheers, Jamie
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-15 02:53 +1000 |
| Message-ID | <53ece976$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76315 |
Jamie Mitchell wrote: > Hello all, > > I want to contour a scatter plot but I don't know how. > > Can anyone help me out? Certainly. Which way did you come in? :-) Sorry, I couldn't resist. It took me literally 20 seconds to find this by googling for "matplotlib contour plot", and it only took that long because I misspelled "contour" the first time. http://matplotlib.org/examples/pylab_examples/contour_demo.html Does this help? If not, please explain what experience you have with matplotlib, what you have tried, what you expected it to do, and what it did instead. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-15 04:54 -0700 |
| Message-ID | <2f9537da-86b8-441d-a214-00123b8416d1@googlegroups.com> |
| In reply to | #76321 |
On Thursday, August 14, 2014 5:53:09 PM UTC+1, Steven D'Aprano wrote:
> Jamie Mitchell wrote:
>
>
>
> > Hello all,
>
> >
>
> > I want to contour a scatter plot but I don't know how.
>
> >
>
> > Can anyone help me out?
>
>
>
> Certainly. Which way did you come in?
>
>
>
> :-)
>
>
>
> Sorry, I couldn't resist.
>
>
>
> It took me literally 20 seconds to find this by googling for "matplotlib
>
> contour plot", and it only took that long because I misspelled "contour"
>
> the first time.
>
>
>
> http://matplotlib.org/examples/pylab_examples/contour_demo.html
>
>
>
>
>
> Does this help? If not, please explain what experience you have with
>
> matplotlib, what you have tried, what you expected it to do, and what it
>
> did instead.
>
>
>
>
>
>
>
> --
>
> Steven
Yep I've seen that thanks but I can't get it to work. I don't have much experience with matplotlib or programming in general.
I just want to get a contour plot of two numpy arrays.
When I call plt.contour on my data I get "input must be a 2D array"
An example of one of my arrays:
array([ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 ,
2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009,
2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014,
1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006,
2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016,
2.16600013, 2.00999999, 1.86200011, 2.19800019, 2.01200008], dtype=float32)
How do I get the above array in to the right format for a contour plot?
Thanks,
Jamie
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-15 23:23 +1000 |
| Message-ID | <53ee09cd$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76365 |
Jamie Mitchell wrote:
[...]
> I just want to get a contour plot of two numpy arrays.
> When I call plt.contour on my data I get "input must be a 2D array"
You are providing a 1D array, or possibly a 3D array. So the question you
really want to ask is not "How do I do contour plots" but "how do I make a
2D array?"
> An example of one of my arrays:
>
> array([ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 ,
> 2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009,
> 2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014,
> 1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006,
> 2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016,
> 2.16600013, 2.00999999, 1.86200011, 2.19800019, 2.01200008],
> dtype=float32)
>
> How do I get the above array in to the right format for a contour plot?
Here's an example of making a 2D array:
py> import numpy
py> a = numpy.array([1.2, 2.5, 3.7, 4.8]) # One dimensional array
py> a
array([ 1.2, 2.5, 3.7, 4.8])
py> b = numpy.array([ [1.2, 2.5, 3.7, 4.8],
... [9.5, 8.1, 7.0, 6.2] ]) # Two dimensional array
py> b
array([[ 1.2, 2.5, 3.7, 4.8],
[ 9.5, 8.1, 7. , 6.2]])
One dimensional arrays are made from a single list of numbers: [...]
Two dimensional arrays are made from a list of lists: [ [...], [...] ]
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-15 07:42 -0700 |
| Message-ID | <c3ca3710-a481-43c2-a2b8-e4105d7160ac@googlegroups.com> |
| In reply to | #76367 |
On Friday, August 15, 2014 2:23:25 PM UTC+1, Steven D'Aprano wrote:
> Jamie Mitchell wrote:
>
>
>
> [...]
>
> > I just want to get a contour plot of two numpy arrays.
>
> > When I call plt.contour on my data I get "input must be a 2D array"
>
>
>
> You are providing a 1D array, or possibly a 3D array. So the question you
>
> really want to ask is not "How do I do contour plots" but "how do I make a
>
> 2D array?"
>
>
>
>
>
> > An example of one of my arrays:
>
> >
>
> > array([ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 ,
>
> > 2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009,
>
> > 2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014,
>
> > 1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006,
>
> > 2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016,
>
> > 2.16600013, 2.00999999, 1.86200011, 2.19800019, 2.01200008],
>
> > dtype=float32)
>
> >
>
> > How do I get the above array in to the right format for a contour plot?
>
>
>
> Here's an example of making a 2D array:
>
>
>
> py> import numpy
>
> py> a = numpy.array([1.2, 2.5, 3.7, 4.8]) # One dimensional array
>
> py> a
>
> array([ 1.2, 2.5, 3.7, 4.8])
>
> py> b = numpy.array([ [1.2, 2.5, 3.7, 4.8],
>
> ... [9.5, 8.1, 7.0, 6.2] ]) # Two dimensional array
>
> py> b
>
> array([[ 1.2, 2.5, 3.7, 4.8],
>
> [ 9.5, 8.1, 7. , 6.2]])
>
>
>
> One dimensional arrays are made from a single list of numbers: [...]
>
>
>
> Two dimensional arrays are made from a list of lists: [ [...], [...] ]
>
>
>
>
>
>
>
> --
>
> Steven
Thank you Steven.
I created the 2D array which read as:
array([[[ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 ,
2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009,
2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014,
1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006,
2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016,
2.16600013, 2.00999999, 1.86200011, 2.19800019, 2.01200008]],
[[ 8.5199995 , 8.88000011, 8.55000019, 7.94999981, 8.60999966,
8.5199995 , 8.80000019, 8.13000011, 8.68999958, 8.72999954,
8.47999954, 8.25 , 8.40999985, 8.43999958, 8.38999939,
8.35999966, 8.63999939, 8.51000023, 8.36999989, 8.69999981,
8.52999973, 8.13999939, 8.36999989, 8.42000008, 8.55999947,
8.72999954, 9.09000015, 8.18999958, 8.76000023, 8.53999996]]], dtype=float32)
Unfortunately when I called plt.contour on this, it said again "Input must be a 2D array".
Is there something I have missed?
Thanks,
Jamie
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-16 01:13 +1000 |
| Message-ID | <53ee2397$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76369 |
Jamie Mitchell wrote:
> I created the 2D array which read as:
That's not a 2D array.
When the amount of data you have is too big to clearly see what it
happening, replace it with something smaller. Instead of 30 items per
sub-array, try it with 5 items per sub-array. Instead of eight decimal
places, try it with single-digit integers. Anything to make it small enough
to see clearly.
When I do that with your data, instead of this:
> array([[[ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 ,
> 2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009,
> 2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014,
> 1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006,
> 2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016,
> 2.16600013, 2.00999999, 1.86200011, 2.19800019,
> 2.01200008]],
>
> [[ 8.5199995 , 8.88000011, 8.55000019, 7.94999981, 8.60999966,
> 8.5199995 , 8.80000019, 8.13000011, 8.68999958, 8.72999954,
> 8.47999954, 8.25 , 8.40999985, 8.43999958, 8.38999939,
> 8.35999966, 8.63999939, 8.51000023, 8.36999989, 8.69999981,
> 8.52999973, 8.13999939, 8.36999989, 8.42000008, 8.55999947,
> 8.72999954, 9.09000015, 8.18999958, 8.76000023,
> 8.53999996]]], dtype=float32)
I get this:
array([[[ 2, 2, 2, 1, 2]],
[[ 8, 8, 8, 7, 8]]], dtype=float32)
which is much easier to work with. See the difference between that smaller
example, and my earlier explanation of the difference between a 1D and 2D
array?
One dimensional arrays are made from a single list of numbers: [...]
Two dimensional arrays are made from a list of lists: [ [...], [...] ]
*Three* dimensional arrays are made from a list of lists of lists:
[ [ [...], [...] ] ]
*Four* dimensional arrays are made from a list of lists of lists of lists:
[ [ [ [...], [...] ] ] ]
and so on. You have a 3D array, with dimensions 2 x 1 x 30.
You can check the dimensions by storing the array into a variable like this:
py> a = numpy.array([[[ 2, 2, 2, 1, 2]], [[ 8, 8, 8, 7, 8]]])
py> a.shape
(2, 1, 5)
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-18 09:51 -0700 |
| Message-ID | <ce4ee9aa-1ea1-45a3-83ff-e4ed017b4c5f@googlegroups.com> |
| In reply to | #76372 |
On Friday, August 15, 2014 4:13:26 PM UTC+1, Steven D'Aprano wrote: > Jamie Mitchell wrote: > > > > > I created the 2D array which read as: > > > > That's not a 2D array. > > > > When the amount of data you have is too big to clearly see what it > > happening, replace it with something smaller. Instead of 30 items per > > sub-array, try it with 5 items per sub-array. Instead of eight decimal > > places, try it with single-digit integers. Anything to make it small enough > > to see clearly. > > > > When I do that with your data, instead of this: > > > > > array([[[ 2.08800006, 2.29400015, 2.00400019, 1.88000011, 2.0480001 , > > > 2.16800022, 2.0480001 , 1.88200009, 1.95800006, 2.00200009, > > > 2.02800012, 1.81200004, 1.95000005, 1.96200013, 1.95200014, > > > 1.99800014, 2.07000017, 1.88200009, 1.98400009, 2.13400006, > > > 2.11400008, 1.89400005, 2.05000019, 2.01999998, 2.03400016, > > > 2.16600013, 2.00999999, 1.86200011, 2.19800019, > > > 2.01200008]], > > > > > > [[ 8.5199995 , 8.88000011, 8.55000019, 7.94999981, 8.60999966, > > > 8.5199995 , 8.80000019, 8.13000011, 8.68999958, 8.72999954, > > > 8.47999954, 8.25 , 8.40999985, 8.43999958, 8.38999939, > > > 8.35999966, 8.63999939, 8.51000023, 8.36999989, 8.69999981, > > > 8.52999973, 8.13999939, 8.36999989, 8.42000008, 8.55999947, > > > 8.72999954, 9.09000015, 8.18999958, 8.76000023, > > > 8.53999996]]], dtype=float32) > > > > > > I get this: > > > > > > array([[[ 2, 2, 2, 1, 2]], > > [[ 8, 8, 8, 7, 8]]], dtype=float32) > > > > > > which is much easier to work with. See the difference between that smaller > > example, and my earlier explanation of the difference between a 1D and 2D > > array? > > > > One dimensional arrays are made from a single list of numbers: [...] > > Two dimensional arrays are made from a list of lists: [ [...], [...] ] > > > > *Three* dimensional arrays are made from a list of lists of lists: > > [ [ [...], [...] ] ] > > > > *Four* dimensional arrays are made from a list of lists of lists of lists: > > [ [ [ [...], [...] ] ] ] > > > > and so on. You have a 3D array, with dimensions 2 x 1 x 30. > > > > You can check the dimensions by storing the array into a variable like this: > > > > py> a = numpy.array([[[ 2, 2, 2, 1, 2]], [[ 8, 8, 8, 7, 8]]]) > > py> a.shape > > (2, 1, 5) > > > > > > > > -- > > Steven Thanks for your suggestions Steven. Unfortunately I still can't make the plot I'm looking for. Do you mind if I go back to the start? Sorry I'm probably not explaining what I need very well. So I have two 1D arrays: 1st array - ([8, 8.8,8.5,7.9,8.6 ...], dtype=float32) It has a shape (150,) 2nd array - ([2, 2.2, 2.5, 2.3, ...],dtype=float32) It has a shape (150,) What I want to do is create a 2D array which merges the 1st and 2nd array so that I would have: ([[8, 8.8,8.5,7.9,8.6 ...],[2,2,2,2,5,2.3, ...]], dtype=float32) that would have a shape (150,150) In this form I could then plot a 2D contour. Thanks for your patience. Jamie
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-18 09:55 -0700 |
| Message-ID | <5a0df87b-6271-48c5-93ec-39949ab7c192@googlegroups.com> |
| In reply to | #76485 |
I forgot to mention that when I try: a=np.array([[hs_con_sw],[te_con_sw]]) I get a 3D shape for some reason - (2,1,150) which is not what I'm after. Thanks, Jamie
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-08-18 10:16 -0700 |
| Message-ID | <2dd6a2f5-7a7d-450c-872d-558f87e0f06a@googlegroups.com> |
| In reply to | #76487 |
On Monday, August 18, 2014 10:25:15 PM UTC+5:30, Jamie Mitchell wrote: > I forgot to mention that when I try: > a=np.array([[hs_con_sw],[te_con_sw]]) > I get a 3D shape for some reason - (2,1,150) which is not what I'm after. I guess you want a=np.array([hs_con_sw,te_con_sw]) ??
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-19 10:10 +1000 |
| Message-ID | <53f295e2$0$29970$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76487 |
Jamie Mitchell wrote: > I forgot to mention that when I try: > > a=np.array([[hs_con_sw],[te_con_sw]]) > > I get a 3D shape for some reason - (2,1,150) which is not what I'm after. No need to wrap the arrays hs_con_sw and te_con_sw in [] lists, since they're already arrays. a = np.array([hs_con_sw, te_con_sw]) ought to do what you want. That's a list [] of arrays, hence two-dimensional. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-19 01:43 -0700 |
| Message-ID | <6bc22f4f-1041-4ad4-9a8f-b0b6ec4bd009@googlegroups.com> |
| In reply to | #76528 |
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? Thanks for all your help, Jamie
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2014-08-19 09:12 -0400 |
| Message-ID | <mailman.13147.1408453949.18130.python-list@python.org> |
| In reply to | #76543 |
On Tue, 19 Aug 2014 01:43:54 -0700 (PDT), Jamie Mitchell
<jamiemitchell1604@gmail.com> declaimed the following:
>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?
>
Sounds like a histogram on a function of (x, y) -- you may need to play
with it to get tolerances suitable for binning, and calculate the Z by
scanning the input data.
What are the domain ranges of X and Y? How many bins in X and Y?
initialize Z array [x-bins, y-bins] <- 0
for (x, y) in zip(x-data, y-data)
convert x and y to bin indices
increment Z[x-index, y-index]
plot Z (mapped to a color scale, dot size, whatever) on a grid of x-bins,
y-bins
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | pecore@pascolo.net |
|---|---|
| Date | 2014-08-19 23:21 +0200 |
| Message-ID | <87iolo2n0z.fsf@pascolo.net> |
| In reply to | #76543 |
Jamie Mitchell <jamiemitchell1604@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
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-08-20 03:02 -0700 |
| Message-ID | <408a7442-e9e0-4e78-b045-f0ea22284e6a@googlegroups.com> |
| In reply to | #76605 |
On Tuesday, August 19, 2014 10:21:48 PM UTC+1, pec...@pascolo.net wrote:
> Jamie Mitchell <jamiemitchell1604@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!!
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-20 17:55 +0100 |
| Message-ID | <mailman.13216.1408553719.18130.python-list@python.org> |
| In reply to | #76651 |
On 20/08/2014 11:02, Jamie Mitchell wrote: > > This is great and works very well - thank you!! > I'm pleased to see that you have answers. In return would you please access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2014-08-18 19:49 +0200 |
| Message-ID | <lsteb8$qtd$1@dont-email.me> |
| In reply to | #76485 |
Am 18.08.14 18:51, schrieb Jamie Mitchell: > On Friday, August 15, 2014 4:13:26 PM UTC+1, Steven D'Aprano wrote: > So I have two 1D arrays: > > 1st array - ([8, 8.8,8.5,7.9,8.6 ...], dtype=float32) > > It has a shape (150,) > > 2nd array - ([2, 2.2, 2.5, 2.3, ...],dtype=float32) > > It has a shape (150,) > > What I want to do is create a 2D array which merges the 1st and 2nd array so that I would have: > > ([[8, 8.8,8.5,7.9,8.6 ...],[2,2,2,2,5,2.3, ...]], dtype=float32) that would have a shape (150,150) There is a logical error in your description. This array would not have the shape (150,150), but (2,150). > In this form I could then plot a 2D contour. An image with 150x150 pixels has in total 150x150=22500 pixels. You've got only 300 datapoints. So it's very unclear what you want to do. A "contour plot" displays lines of equal height over a two-dimensional dataset, as in a topographic map. You don't seem to have a two-dimensional dataset at first place; maybe you already have *contour data*, and you just want to plot(x,y) ? Christian
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2014-08-18 13:37 +0300 |
| Message-ID | <vg3r40e84nq.fsf@coffee.modeemi.fi> |
| In reply to | #76369 |
Jamie Mitchell <jamiemitchell1604@gmail.com> writes: > I created the 2D array which read as: Maybe you could try numpy.reshape() on your 1D array?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web