|
From: Andreas H. <li...@hi...> - 2012-02-28 09:42:27
Attachments:
amf-changes_profile.png
|
Good morning, I'm creating the attached plot using pcolormesh(). What I would like to do now is draw contour lines at +/- 2.5%, which follow the grid edges. The problem is that when I use contour(), the lines drawn do not follow the grid edges but seem to be interpolated somehow. Do you have an idea how to draw the contour lines following the grid edges? Your insight is very much appreciated :) Cheers, Andreas. |
|
From: Benjamin R. <ben...@ou...> - 2012-02-28 15:11:06
|
On Tuesday, February 28, 2012, Andreas H. wrote: > Good morning, > > I'm creating the attached plot using pcolormesh(). What I would like to > do now is draw contour lines at +/- 2.5%, which follow the grid edges. > > The problem is that when I use contour(), the lines drawn do not follow > the grid edges but seem to be interpolated somehow. > > Do you have an idea how to draw the contour lines following the grid edges? > > Your insight is very much appreciated :) > > Cheers, > Andreas. > This is because of a subtle difference in how pcolor-like functions and contour-like functions work. I always forget which is which, but one assumes that the z value lies on the vertices of the grid while the other assumes that it lies in the middle of each grid point. This is why you see them slightly offset from each other. I hope that clarifies everything for you. Ben Root |
|
From: Ryan M. <rm...@gm...> - 2012-02-28 15:20:12
|
On Tue, Feb 28, 2012 at 9:10 AM, Benjamin Root <ben...@ou...> wrote: > On Tuesday, February 28, 2012, Andreas H. wrote: >> >> Good morning, >> >> I'm creating the attached plot using pcolormesh(). What I would like to >> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >> >> The problem is that when I use contour(), the lines drawn do not follow >> the grid edges but seem to be interpolated somehow. >> >> Do you have an idea how to draw the contour lines following the grid >> edges? >> >> Your insight is very much appreciated :) >> >> Cheers, >> Andreas. > > > This is because of a subtle difference in how pcolor-like functions and > contour-like functions work. I always forget which is which, but one > assumes that the z value lies on the vertices of the grid while the other > assumes that it lies in the middle of each grid point. This is why you see > them slightly offset from each other. By definitition, with pcolormesh you're giving it the edges of the grid cells. In fact, for an NxN grid of data, you should have N+1xN+1 grids of x,y values. Contour is given the actual grid and values. Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma |
|
From: Andreas H. <li...@hi...> - 2012-02-28 15:22:26
Attachments:
amf-changes_profile_centre.png
|
> On Tuesday, February 28, 2012, Andreas H. wrote:
>
>> Good morning,
>>
>> I'm creating the attached plot using pcolormesh(). What I would like to
>> do now is draw contour lines at +/- 2.5%, which follow the grid edges.
>>
>> The problem is that when I use contour(), the lines drawn do not follow
>> the grid edges but seem to be interpolated somehow.
>>
>> Do you have an idea how to draw the contour lines following the grid
>> edges?
>>
>> Your insight is very much appreciated :)
>>
>> Cheers,
>> Andreas.
>>
>
> This is because of a subtle difference in how pcolor-like functions and
> contour-like functions work. I always forget which is which, but one
> assumes that the z value lies on the vertices of the grid while the other
> assumes that it lies in the middle of each grid point. This is why you
> see
> them slightly offset from each other.
Thanks, Ben!
To `pcolormesh`, I pass the *edges* of the grid:
xbin = linspace(0, 12, nxbin + 1)
ybin = np.linspace(-90, 90, nybin + 1)
pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None',
vmin=-5, vmax=20)
`contour`, however, wants the coordinates themselves. So I do
spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T,
[-2.5, 2.5])
Still, the outcome is, well, unexpected to me. Actually, no matter if
contour wants centres or edges, the actual behaviour seems strange. There
is some interpolation going on, apparently. The input `pdata` has shape
(12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid
movement in the x-direction.
Any ideas?
Cheers,
Andreas. |
|
From: Andreas H. <li...@hi...> - 2012-02-28 16:28:56
|
>> On Tuesday, February 28, 2012, Andreas H. wrote: >> >>> Good morning, >>> >>> I'm creating the attached plot using pcolormesh(). What I would like to >>> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >>> >>> The problem is that when I use contour(), the lines drawn do not follow >>> the grid edges but seem to be interpolated somehow. >>> >>> Do you have an idea how to draw the contour lines following the grid >>> edges? >>> >>> Your insight is very much appreciated :) >>> >>> Cheers, >>> Andreas. >>> >> >> This is because of a subtle difference in how pcolor-like functions and >> contour-like functions work. I always forget which is which, but one >> assumes that the z value lies on the vertices of the grid while the >> other >> assumes that it lies in the middle of each grid point. This is why you >> see >> them slightly offset from each other. > > Thanks, Ben! > > To `pcolormesh`, I pass the *edges* of the grid: > > xbin = linspace(0, 12, nxbin + 1) > ybin = np.linspace(-90, 90, nybin + 1) > > pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None', > vmin=-5, vmax=20) > > `contour`, however, wants the coordinates themselves. So I do > > spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T, > [-2.5, 2.5]) > > Still, the outcome is, well, unexpected to me. Actually, no matter if > contour wants centres or edges, the actual behaviour seems strange. There > is some interpolation going on, apparently. The input `pdata` has shape > (12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid > movement in the x-direction. > > Any ideas? Okay, after some diving into matplotlib sources, I guess the interpolation comes within the function `QuadContourSet._get_allsegs_and_allkinds`. So there seems to be no way to accomplish what I actually want with the current matplotlib API. Correct? If I wanted to do something about this, I would need to * implement a class `GriddedContourSet`, derived from `ContourSet`, where I implement the `_get_allsegs_and_allkinds` method appropriately. * add an additional keyword argument to `contour()` to make this gridded contourset an option when calling `contour()`. Is this all correct? If yes, I might start working on this if I get the time ... Cheers, Andreas. |
|
From: Eric F. <ef...@ha...> - 2012-02-28 17:57:07
|
On 02/28/2012 06:28 AM, Andreas H. wrote: >>> On Tuesday, February 28, 2012, Andreas H. wrote: >>> >>>> Good morning, >>>> >>>> I'm creating the attached plot using pcolormesh(). What I would like to >>>> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >>>> >>>> The problem is that when I use contour(), the lines drawn do not follow >>>> the grid edges but seem to be interpolated somehow. >>>> >>>> Do you have an idea how to draw the contour lines following the grid >>>> edges? >>>> >>>> Your insight is very much appreciated :) >>>> >>>> Cheers, >>>> Andreas. >>>> >>> >>> This is because of a subtle difference in how pcolor-like functions and >>> contour-like functions work. I always forget which is which, but one >>> assumes that the z value lies on the vertices of the grid while the >>> other >>> assumes that it lies in the middle of each grid point. This is why you >>> see >>> them slightly offset from each other. >> >> Thanks, Ben! >> >> To `pcolormesh`, I pass the *edges* of the grid: >> >> xbin = linspace(0, 12, nxbin + 1) >> ybin = np.linspace(-90, 90, nybin + 1) >> >> pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None', >> vmin=-5, vmax=20) >> >> `contour`, however, wants the coordinates themselves. So I do >> >> spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T, >> [-2.5, 2.5]) >> >> Still, the outcome is, well, unexpected to me. Actually, no matter if >> contour wants centres or edges, the actual behaviour seems strange. There >> is some interpolation going on, apparently. The input `pdata` has shape >> (12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid >> movement in the x-direction. >> >> Any ideas? > > Okay, after some diving into matplotlib sources, I guess the interpolation > comes within the function `QuadContourSet._get_allsegs_and_allkinds`. So > there seems to be no way to accomplish what I actually want with the > current matplotlib API. Correct? > > If I wanted to do something about this, I would need to > > * implement a class `GriddedContourSet`, derived from `ContourSet`, where > I implement the `_get_allsegs_and_allkinds` method appropriately. > * add an additional keyword argument to `contour()` to make this gridded > contourset an option when calling `contour()`. > > Is this all correct? If yes, I might start working on this if I get the > time ... It is not at all clear to me what you want to do, as compared to what contour does. Can you illustrate with an extremely simple example? Maybe even a scanned sketch, if necessary? Do you want the contour lines to be stepped, like the rectilinear boundaries of the pcolormesh cells--that is, composed entirely of horizontal and vertical line segments? Eric > > Cheers, > Andreas. > > > > > ------------------------------------------------------------------------------ > Keep Your Developer Skills Current with LearnDevNow! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-d2d > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
|
From: Andreas H. <li...@hi...> - 2012-02-28 18:08:36
Attachments:
example.pdf
|
Am 28.02.2012 18:56, schrieb Eric Firing: > On 02/28/2012 06:28 AM, Andreas H. wrote: >>>> On Tuesday, February 28, 2012, Andreas H. wrote: >>>> >>>>> Good morning, >>>>> >>>>> I'm creating the attached plot using pcolormesh(). What I would like to >>>>> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >>>>> >>>>> The problem is that when I use contour(), the lines drawn do not follow >>>>> the grid edges but seem to be interpolated somehow. >>>>> >>>>> Do you have an idea how to draw the contour lines following the grid >>>>> edges? >>>>> >>>>> Your insight is very much appreciated :) >>>>> >>>>> Cheers, >>>>> Andreas. >>>>> >>>> >>>> This is because of a subtle difference in how pcolor-like functions and >>>> contour-like functions work. I always forget which is which, but one >>>> assumes that the z value lies on the vertices of the grid while the >>>> other >>>> assumes that it lies in the middle of each grid point. This is why you >>>> see >>>> them slightly offset from each other. >>> >>> Thanks, Ben! >>> >>> To `pcolormesh`, I pass the *edges* of the grid: >>> >>> xbin = linspace(0, 12, nxbin + 1) >>> ybin = np.linspace(-90, 90, nybin + 1) >>> >>> pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None', >>> vmin=-5, vmax=20) >>> >>> `contour`, however, wants the coordinates themselves. So I do >>> >>> spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T, >>> [-2.5, 2.5]) >>> >>> Still, the outcome is, well, unexpected to me. Actually, no matter if >>> contour wants centres or edges, the actual behaviour seems strange. There >>> is some interpolation going on, apparently. The input `pdata` has shape >>> (12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid >>> movement in the x-direction. >>> >>> Any ideas? >> >> Okay, after some diving into matplotlib sources, I guess the interpolation >> comes within the function `QuadContourSet._get_allsegs_and_allkinds`. So >> there seems to be no way to accomplish what I actually want with the >> current matplotlib API. Correct? >> >> If I wanted to do something about this, I would need to >> >> * implement a class `GriddedContourSet`, derived from `ContourSet`, where >> I implement the `_get_allsegs_and_allkinds` method appropriately. >> * add an additional keyword argument to `contour()` to make this gridded >> contourset an option when calling `contour()`. >> >> Is this all correct? If yes, I might start working on this if I get the >> time ... > > It is not at all clear to me what you want to do, as compared to what > contour does. Can you illustrate with an extremely simple example? > Maybe even a scanned sketch, if necessary? Do you want the contour lines > to be stepped, like the rectilinear boundaries of the pcolormesh > cells--that is, composed entirely of horizontal and vertical line segments? Yes, Eric, that's exactly what I want. Since my case was simple enough, I did it completely manually, with loads of calls to `plot` (I'm sure there would've been a simpler solution ... -- which one?). I attached the plot so you get an idea of what I want to do. Thanks for your help! Andreas. |
|
From: Eric F. <ef...@ha...> - 2012-02-28 18:24:58
|
On 02/28/2012 08:08 AM, Andreas H. wrote: > Am 28.02.2012 18:56, schrieb Eric Firing: >> On 02/28/2012 06:28 AM, Andreas H. wrote: >>>>> On Tuesday, February 28, 2012, Andreas H. wrote: >>>>> >>>>>> Good morning, >>>>>> >>>>>> I'm creating the attached plot using pcolormesh(). What I would like to >>>>>> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >>>>>> >>>>>> The problem is that when I use contour(), the lines drawn do not follow >>>>>> the grid edges but seem to be interpolated somehow. >>>>>> >>>>>> Do you have an idea how to draw the contour lines following the grid >>>>>> edges? >>>>>> >>>>>> Your insight is very much appreciated :) >>>>>> >>>>>> Cheers, >>>>>> Andreas. >>>>>> >>>>> >>>>> This is because of a subtle difference in how pcolor-like functions and >>>>> contour-like functions work. I always forget which is which, but one >>>>> assumes that the z value lies on the vertices of the grid while the >>>>> other >>>>> assumes that it lies in the middle of each grid point. This is why you >>>>> see >>>>> them slightly offset from each other. >>>> >>>> Thanks, Ben! >>>> >>>> To `pcolormesh`, I pass the *edges* of the grid: >>>> >>>> xbin = linspace(0, 12, nxbin + 1) >>>> ybin = np.linspace(-90, 90, nybin + 1) >>>> >>>> pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None', >>>> vmin=-5, vmax=20) >>>> >>>> `contour`, however, wants the coordinates themselves. So I do >>>> >>>> spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T, >>>> [-2.5, 2.5]) >>>> >>>> Still, the outcome is, well, unexpected to me. Actually, no matter if >>>> contour wants centres or edges, the actual behaviour seems strange. There >>>> is some interpolation going on, apparently. The input `pdata` has shape >>>> (12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid >>>> movement in the x-direction. >>>> >>>> Any ideas? >>> >>> Okay, after some diving into matplotlib sources, I guess the interpolation >>> comes within the function `QuadContourSet._get_allsegs_and_allkinds`. So >>> there seems to be no way to accomplish what I actually want with the >>> current matplotlib API. Correct? >>> >>> If I wanted to do something about this, I would need to >>> >>> * implement a class `GriddedContourSet`, derived from `ContourSet`, where >>> I implement the `_get_allsegs_and_allkinds` method appropriately. >>> * add an additional keyword argument to `contour()` to make this gridded >>> contourset an option when calling `contour()`. >>> >>> Is this all correct? If yes, I might start working on this if I get the >>> time ... >> >> It is not at all clear to me what you want to do, as compared to what >> contour does. Can you illustrate with an extremely simple example? >> Maybe even a scanned sketch, if necessary? Do you want the contour lines >> to be stepped, like the rectilinear boundaries of the pcolormesh >> cells--that is, composed entirely of horizontal and vertical line segments? > > Yes, Eric, that's exactly what I want. Since my case was simple enough, > I did it completely manually, with loads of calls to `plot` (I'm sure > there would've been a simpler solution ... -- which one?). I attached > the plot so you get an idea of what I want to do. Andreas, I have never seen a contour algorithm with an option to do that, but I understand the motivation. I don't think it would be easy to implement; contouring algorithms generally are not. You might get an adequate approximation by using nearest-neighbor interpolation to interpolate your data to a very fine grid, and then contour that. Eric > > Thanks for your help! > Andreas. > > > > ------------------------------------------------------------------------------ > Keep Your Developer Skills Current with LearnDevNow! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-d2d > > > > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
|
From: Andreas H. <li...@hi...> - 2012-02-28 19:41:37
|
Am Di 28 Feb 2012 19:23:14 CET schrieb Eric Firing: > On 02/28/2012 08:08 AM, Andreas H. wrote: >> Am 28.02.2012 18:56, schrieb Eric Firing: >>> On 02/28/2012 06:28 AM, Andreas H. wrote: >>>>>> On Tuesday, February 28, 2012, Andreas H. wrote: >>>>>> >>>>>>> Good morning, >>>>>>> >>>>>>> I'm creating the attached plot using pcolormesh(). What I would like to >>>>>>> do now is draw contour lines at +/- 2.5%, which follow the grid edges. >>>>>>> >>>>>>> The problem is that when I use contour(), the lines drawn do not follow >>>>>>> the grid edges but seem to be interpolated somehow. >>>>>>> >>>>>>> Do you have an idea how to draw the contour lines following the grid >>>>>>> edges? >>>>>>> >>>>>>> Your insight is very much appreciated :) >>>>>>> >>>>>>> Cheers, >>>>>>> Andreas. >>>>>>> >>>>>> >>>>>> This is because of a subtle difference in how pcolor-like functions and >>>>>> contour-like functions work. I always forget which is which, but one >>>>>> assumes that the z value lies on the vertices of the grid while the >>>>>> other >>>>>> assumes that it lies in the middle of each grid point. This is why you >>>>>> see >>>>>> them slightly offset from each other. >>>>> >>>>> Thanks, Ben! >>>>> >>>>> To `pcolormesh`, I pass the *edges* of the grid: >>>>> >>>>> xbin = linspace(0, 12, nxbin + 1) >>>>> ybin = np.linspace(-90, 90, nybin + 1) >>>>> >>>>> pl = spl.pcolormesh(xbin, ybin, pdata.T, cmap=cmap, edgecolors='None', >>>>> vmin=-5, vmax=20) >>>>> >>>>> `contour`, however, wants the coordinates themselves. So I do >>>>> >>>>> spl.contour((xbin[:-1]+xbin[1:])/2., (ybin[:-1]+ybin[1:])/2, pdata.T, >>>>> [-2.5, 2.5]) >>>>> >>>>> Still, the outcome is, well, unexpected to me. Actually, no matter if >>>>> contour wants centres or edges, the actual behaviour seems strange. There >>>>> is some interpolation going on, apparently. The input `pdata` has shape >>>>> (12, 72) (or 72,12), and I definitely wouldn't expect this sub-grid >>>>> movement in the x-direction. >>>>> >>>>> Any ideas? >>>> >>>> Okay, after some diving into matplotlib sources, I guess the interpolation >>>> comes within the function `QuadContourSet._get_allsegs_and_allkinds`. So >>>> there seems to be no way to accomplish what I actually want with the >>>> current matplotlib API. Correct? >>>> >>>> If I wanted to do something about this, I would need to >>>> >>>> * implement a class `GriddedContourSet`, derived from `ContourSet`, where >>>> I implement the `_get_allsegs_and_allkinds` method appropriately. >>>> * add an additional keyword argument to `contour()` to make this gridded >>>> contourset an option when calling `contour()`. >>>> >>>> Is this all correct? If yes, I might start working on this if I get the >>>> time ... >>> >>> It is not at all clear to me what you want to do, as compared to what >>> contour does. Can you illustrate with an extremely simple example? >>> Maybe even a scanned sketch, if necessary? Do you want the contour lines >>> to be stepped, like the rectilinear boundaries of the pcolormesh >>> cells--that is, composed entirely of horizontal and vertical line segments? >> >> Yes, Eric, that's exactly what I want. Since my case was simple enough, >> I did it completely manually, with loads of calls to `plot` (I'm sure >> there would've been a simpler solution ... -- which one?). I attached >> the plot so you get an idea of what I want to do. > > Andreas, > > I have never seen a contour algorithm with an option to do that, but I > understand the motivation. I don't think it would be easy to implement; > contouring algorithms generally are not. > > You might get an adequate approximation by using nearest-neighbor > interpolation to interpolate your data to a very fine grid, and then > contour that. Eric, thanks, that's a good hint. I took a look into the C source of the contour algorithm and decided not to bother right now. But your suggestion should do it. Cheers, Andreas. |
|
From: Moore, E. (NIH/N. [F] <eri...@ni...> - 2012-02-28 22:36:39
Attachments:
lattice_cntr_demo.png
lattice_cntr_demo.py
|
I think that what you want can be achieved more simply than contouring. I've attached a quick example of how I would do this. I'm assuming that the data has the same form as pcolormesh, i.e, the z values are measured at grid centers but we have coordinate of the edges. This means that we can easily check if a given pixel is above or below the contour level and then we simply draw the appropriate gridlines. I'm not coalescing neighboring lines, but that's also possible. If you're looking for something more complex than this simple in-or-out partitioning, this approach might still work but I'm not sure, in that case, that I understand what you're actually trying to do. -Eric p.s. two loops aren't necessary, really. |
|
From: Andreas H. <li...@hi...> - 2012-02-29 07:28:03
|
> I think that what you want can be achieved more simply than contouring. > I've attached a quick example of how I would do this. I'm assuming that > the data has the same form as pcolormesh, i.e, the z values are measured > at grid centers but we have coordinate of the edges. This means that we > can easily check if a given pixel is above or below the contour level and > then we simply draw the appropriate gridlines. I'm not coalescing > neighboring lines, but that's also possible. > > If you're looking for something more complex than this simple in-or-out > partitioning, this approach might still work but I'm not sure, in that > case, that I understand what you're actually trying to do. Eric, I'm amazed -- thanks a lot :) Andreas. |