|
From: Mike A. <mik...@ng...> - 2009-11-23 21:42:45
|
This may be a dumb question, however i have been scratching my head trying to figure out how to plot a 3 dimensional plot with with a colour map different from the elevation(Z) parameter. An example of this done in Matlab would be [X,Y,Z] = peaks(30); C=Z'% could be anything other than Z as long as it has the same dimensions surf(X,Y,Z,C) axis([-3 3 -3 3 -10 5]) Is this possible with matplotlib '0.99.1' If so how do i go about doing this is there some sample code? Mike Alger, M.A.Sc ma...@ry... |
|
From: Mike A. <ma...@ry...> - 2009-11-26 02:41:54
|
I have been looking at this for the past day and in am pretty sure I could
replace the instance of polyc by the "cmap if statements" my colour array
and I should be able to get close to what I want. However I am new to both
python & mpl, and I am not entirely sure in how I would go about testing my
hypothesis. Furthermore I am also relatively new to submitting fixes to
open-source projects so I have lots of questions about how I would go about
suggesting a modification.
1.) can I just modify the file in the
C:\python26\Lib\site-packages\mpl-toolkits\mplot3d\axes3d.py file to do my
tests?
a. Also, where are these files usually kept in a linux environment ?
b. What do I do with the. pyc files with the same name? are they
re-complied automatically when I call the function externally?
2.) Is this capability already built in with the colour argument ? if so
how do I properly call it?
3.) If I do make a modification should it be as a separate function with
the additional variable or should I try to stuff the new capability into the
old function
4.) is there a clean easy to follow tutorial for submitting changes via
svn or can I rely on someone else to do the final commit?
I have attached the function in question for reference to save others from
digging down into their python directories
Again thanks for taking your time to help me figure this out
Mike Alger
< Code>
def plot_surface(self, X, Y, Z, *args, **kwargs):
'''
Create a surface plot.
By default it will be colored in shades of a solid color,
but it also supports color mapping by supplying the *cmap*
argument.
========== ================================================
Argument Description
========== ================================================
*X*, *Y*, Data values as numpy.arrays
*Z*
*rstride* Array row stride (step size)
*cstride* Array column stride (step size)
*color* Color of the surface patches
*cmap* A colormap for the surface patches.
========== ================================================
'''
had_data = self.has_data()
rows, cols = Z.shape
tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
rstride = kwargs.pop('rstride', 10)
cstride = kwargs.pop('cstride', 10)
color = kwargs.pop('color', 'b')
color = np.array(colorConverter.to_rgba(color))
cmap = kwargs.get('cmap', None)
polys = []
normals = []
avgz = []
for rs in np.arange(0, rows-1, rstride):
for cs in np.arange(0, cols-1, cstride):
ps = []
corners = []
for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
ztop = a[rs][cs:min(cols, cs+cstride+1)]
zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
rs+rstride+1)]
zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
cs+cstride+1):]
zbase = zbase[::-1]
zright = ta[cs][rs:min(rows, rs+rstride+1):]
zright = zright[::-1]
corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
z = np.concatenate((ztop, zleft, zbase, zright))
ps.append(z)
# The construction leaves the array with duplicate points,
which
# are removed here.
ps = zip(*ps)
lastp = np.array([])
ps2 = []
avgzsum = 0.0
for p in ps:
if p != lastp:
ps2.append(p)
lastp = p
avgzsum += p[2]
polys.append(ps2)
avgz.append(avgzsum / len(ps2))
v1 = np.array(ps2[0]) - np.array(ps2[1])
v2 = np.array(ps2[2]) - np.array(ps2[0])
normals.append(np.cross(v1, v2))
polyc = art3d.Poly3DCollection(polys, *args, **kwargs) ## this is
where a modification could be made to allow for a separate colour matrix
if cmap is not None:
polyc.set_array(np.array(avgz))
polyc.set_linewidth(0)
else:
colors = self._shade_colors(color, normals)
polyc.set_facecolors(colors)
self.add_collection(polyc)
self.auto_scale_xyz(X, Y, Z, had_data)
return polyc
</Code>
From: Mike Alger [mailto:mik...@ng...]
Sent: November-23-09 3:42 PM
To: mat...@li...
Subject: [Matplotlib-users] Color in 3d plots
This may be a dumb question, however I have been scratching my head trying
to figure out how to plot a 3 dimensional plot with with a colour map
different from the elevation(Z) parameter.
An example of this done in Matlab would be
[X,Y,Z] = peaks(30);
C=Z'% could be anything other than Z as long as it has the same dimensions
surf(X,Y,Z,C)
axis([-3 3 -3 3 -10 5])
Is this possible with matplotlib '0.99.1'
If so how do i go about doing this is there some sample code?
Mike Alger, M.A.Sc
ma...@ry...
|
|
From: Mike A. <ma...@ry...> - 2009-12-01 02:06:42
|
After a weekend of no replies I managed to figure a way out myself
As this was "left to the reader as an exercise" I will leave the integration
or improvement of this solution as an exercise to the next reader
What I have done is basically cloned the plot surface function and replaced
the avgz variable with a reference to the "colors" parameter i have added to
the function call.
This code doesn't center things perfectly with respect to the grid (for
some reason a 40x40 grid turns into 39x39 grid in the function) again this
is something else that could be improved, however I am happy with it and a
one pixel shift won't be missed in my plots. I also have no real clue as to
what the following comments was about
# The construction leaves the array with duplicate points,
which
# are removed here.
but it is probably related to my non centered plots.
What follows is the modified function :
def plot_surface2(self, X, Y, Z, colors, *args, **kwargs):
'''
Create a surface plot.
By default it will be colored in shades of a solid color,
but it also supports color mapping by supplying the *cmap*
argument.
========== ================================================
Argument Description
========== ================================================
*X*, *Y*, Data values as numpy.arrays
*Z*
*colors* an array the same size as z that contains a separate
color data
*rstride* Array row stride (step size)
*cstride* Array column stride (step size)
*color* Color of the surface patches
*cmap* A colormap for the surface patches.
========== ================================================
'''
had_data = self.has_data()
rows, cols = Z.shape
tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
rstride = kwargs.pop('rstride', 10)
cstride = kwargs.pop('cstride', 10)
color = kwargs.pop('color', 'b')
color = np.array(colorConverter.to_rgba(color))
cmap = kwargs.get('cmap', None)
polys = []
normals = []
avgz = []
for rs in np.arange(0, rows-1, rstride):
for cs in np.arange(0, cols-1, cstride):
ps = []
corners = []
for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
ztop = a[rs][cs:min(cols, cs+cstride+1)]
zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
rs+rstride+1)]
zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
cs+cstride+1):]
zbase = zbase[::-1]
zright = ta[cs][rs:min(rows, rs+rstride+1):]
zright = zright[::-1]
corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
z = np.concatenate((ztop, zleft, zbase, zright))
ps.append(z)
# The construction leaves the array with duplicate points,
which
# are removed here.
ps = zip(*ps)
lastp = np.array([])
ps2 = []
avgzsum = 0.0
for p in ps:
if p != lastp:
ps2.append(p)
lastp = p
avgzsum += p[2]
polys.append(ps2)
##################################
Begin of changes
##################################
#avgz.append(avgzsum / len(ps2))
avgz.append(colors[rs][cs])
##################################
end of changes
##################################
v1 = np.array(ps2[0]) - np.array(ps2[1])
v2 = np.array(ps2[2]) - np.array(ps2[0])
normals.append(np.cross(v1, v2))
polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
if cmap is not None:
# polyc.set_array(np.array(colors))
polyc.set_array(np.array(avgz))
polyc.set_linewidth(0)
else:
colors = self._shade_colors(color, normals)
polyc.set_facecolors(colors)
self.add_collection(polyc)
self.auto_scale_xyz(X, Y, Z, had_data)
return polyc
From: Mike Alger [mailto:ma...@ry...]
Sent: November-25-09 8:42 PM
To: mat...@li...
Subject: Re: [Matplotlib-users] Color in 3d plots
I have been looking at this for the past day and in am pretty sure I could
replace the instance of polyc by the "cmap if statements" my colour array
and I should be able to get close to what I want. However I am new to both
python & mpl, and I am not entirely sure in how I would go about testing my
hypothesis. Furthermore I am also relatively new to submitting fixes to
open-source projects so I have lots of questions about how I would go about
suggesting a modification.
1.) can I just modify the file in the
C:\python26\Lib\site-packages\mpl-toolkits\mplot3d\axes3d.py file to do my
tests?
a. Also, where are these files usually kept in a linux environment ?
b. What do I do with the. pyc files with the same name? are they
re-complied automatically when I call the function externally?
2.) Is this capability already built in with the colour argument ? if so
how do I properly call it?
3.) If I do make a modification should it be as a separate function with
the additional variable or should I try to stuff the new capability into the
old function
4.) is there a clean easy to follow tutorial for submitting changes via
svn or can I rely on someone else to do the final commit?
I have attached the function in question for reference to save others from
digging down into their python directories
Again thanks for taking your time to help me figure this out
Mike Alger
< Code>
def plot_surface(self, X, Y, Z, *args, **kwargs):
'''
Create a surface plot.
By default it will be colored in shades of a solid color,
but it also supports color mapping by supplying the *cmap*
argument.
========== ================================================
Argument Description
========== ================================================
*X*, *Y*, Data values as numpy.arrays
*Z*
*rstride* Array row stride (step size)
*cstride* Array column stride (step size)
*color* Color of the surface patches
*cmap* A colormap for the surface patches.
========== ================================================
'''
had_data = self.has_data()
rows, cols = Z.shape
tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
rstride = kwargs.pop('rstride', 10)
cstride = kwargs.pop('cstride', 10)
color = kwargs.pop('color', 'b')
color = np.array(colorConverter.to_rgba(color))
cmap = kwargs.get('cmap', None)
polys = []
normals = []
avgz = []
for rs in np.arange(0, rows-1, rstride):
for cs in np.arange(0, cols-1, cstride):
ps = []
corners = []
for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
ztop = a[rs][cs:min(cols, cs+cstride+1)]
zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
rs+rstride+1)]
zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
cs+cstride+1):]
zbase = zbase[::-1]
zright = ta[cs][rs:min(rows, rs+rstride+1):]
zright = zright[::-1]
corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
z = np.concatenate((ztop, zleft, zbase, zright))
ps.append(z)
# The construction leaves the array with duplicate points,
which
# are removed here.
ps = zip(*ps)
lastp = np.array([])
ps2 = []
avgzsum = 0.0
for p in ps:
if p != lastp:
ps2.append(p)
lastp = p
avgzsum += p[2]
polys.append(ps2)
avgz.append(avgzsum / len(ps2))
v1 = np.array(ps2[0]) - np.array(ps2[1])
v2 = np.array(ps2[2]) - np.array(ps2[0])
normals.append(np.cross(v1, v2))
polyc = art3d.Poly3DCollection(polys, *args, **kwargs) ## this is
where a modification could be made to allow for a separate colour matrix
if cmap is not None:
polyc.set_array(np.array(avgz))
polyc.set_linewidth(0)
else:
colors = self._shade_colors(color, normals)
polyc.set_facecolors(colors)
self.add_collection(polyc)
self.auto_scale_xyz(X, Y, Z, had_data)
return polyc
</Code>
From: Mike Alger [mailto:mik...@ng...]
Sent: November-23-09 3:42 PM
To: mat...@li...
Subject: [Matplotlib-users] Color in 3d plots
This may be a dumb question, however I have been scratching my head trying
to figure out how to plot a 3 dimensional plot with with a colour map
different from the elevation(Z) parameter.
An example of this done in Matlab would be
[X,Y,Z] = peaks(30);
C=Z'% could be anything other than Z as long as it has the same dimensions
surf(X,Y,Z,C)
axis([-3 3 -3 3 -10 5])
Is this possible with matplotlib '0.99.1'
If so how do i go about doing this is there some sample code?
Mike Alger, M.A.Sc
ma...@ry...
|
|
From: John H. <jd...@gm...> - 2009-12-01 04:26:19
|
On Mon, Nov 30, 2009 at 8:06 PM, Mike Alger <ma...@ry...> wrote: > After a weekend of no replies I managed to figure a way out myself Hey Mike, sorry for the radio silence. mpl is a big project and no one developer is equipped to answer questions about everything. We currently have only one active developer (Reinier) working on the 3D stuff. I've CCd him, and hopefully he can take a look at your patch. > > 3.) If I do make a modification should it be as a separate function with > the additional variable or should I try to stuff the new capability into the > old function > > 4.) is there a clean easy to follow tutorial for submitting changes via > svn or can I rely on someone else to do the final commit? In general, if the new functionality is close to the old, we'd like to see it incorporated into the existing API, perhaps with a new keyword argument. We have some documentation on how to contribute to mpl at http://matplotlib.sourceforge.net/devel/coding_guide.html; see also the FAQ http://matplotlib.sourceforge.net/faq/howto_faq.html#contributing-howto > As this was “left to the reader as an exercise” I will leave the integration > or improvement of this solution as an exercise to the next reader > > What I have done is basically cloned the plot surface function and replaced > the avgz variable with a reference to the “colors” parameter i have added to > the function call. > > This code doesn’t center things perfectly with respect to the grid (for > some reason a 40x40 grid turns into 39x39 grid in the function) again this > is something else that could be improved, however I am happy with it and a > one pixel shift won’t be missed in my plots. I also have no real clue as to > what the following comments was about > > > > # The construction leaves the array with duplicate points, > which > > # are removed here. > > > > but it is probably related to my non centered plots. > > What follows is the modified function : > > def plot_surface2(self, X, Y, Z, colors, *args, **kwargs): What will be most helpful is an "svn diff", as explained in the coding guide and FAQ linked above, with an example (included in the diff) that shows the before and after behavior. That way even an naive developer can appreciate the before and after changes and commit the code if the original developer responsible for that part of the code base is not available. The ideal situation is "apply this patch generated from an svn diff and run example so_and_so.py to see the plot with and without the patch". As explained in the FAQ, if you don't get proper attention here on the mailing list, please post a bug or patch on the sourceforge tracker so we don't lose it -- sometimes our inattention is not due to lack of interest but to lack of time, and a report filed on the tracker helps us not lose the thread. Thanks for the help! JDH |
|
From: Reinier H. <re...@he...> - 2009-12-10 00:44:42
|
Hi Mike,
Sorry for the slow reply, but I put support for this in the
development version in SVN. It can also do a bit of shading to make
the surface look more structured.
Note that the fact that a 40x40 grid turns into 39x39 squares is
expected behavior: the code assumes the 40 points are the *edges* of
the patches. There are only 39 patches between 40 points.
Regards,
Reinier
On Tue, Dec 1, 2009 at 3:06 AM, Mike Alger <ma...@ry...> wrote:
> After a weekend of no replies I managed to figure a way out myself
>
> As this was “left to the reader as an exercise” I will leave the integration
> or improvement of this solution as an exercise to the next reader
>
> What I have done is basically cloned the plot surface function and replaced
> the avgz variable with a reference to the “colors” parameter i have added to
> the function call.
>
> This code doesn’t center things perfectly with respect to the grid (for
> some reason a 40x40 grid turns into 39x39 grid in the function) again this
> is something else that could be improved, however I am happy with it and a
> one pixel shift won’t be missed in my plots. I also have no real clue as to
> what the following comments was about
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
>
>
> but it is probably related to my non centered plots.
>
>
>
>
>
>
>
>
>
> What follows is the modified function :
>
>
>
>
>
>
>
> def plot_surface2(self, X, Y, Z, colors, *args, **kwargs):
>
> '''
>
> Create a surface plot.
>
>
>
> By default it will be colored in shades of a solid color,
>
> but it also supports color mapping by supplying the *cmap*
>
> argument.
>
>
>
> ========== ================================================
>
> Argument Description
>
> ========== ================================================
>
> *X*, *Y*, Data values as numpy.arrays
>
> *Z*
>
> *colors* an array the same size as z that contains a separate
> color data
>
> *rstride* Array row stride (step size)
>
> *cstride* Array column stride (step size)
>
> *color* Color of the surface patches
>
> *cmap* A colormap for the surface patches.
>
> ========== ================================================
>
> '''
>
>
>
> had_data = self.has_data()
>
>
>
> rows, cols = Z.shape
>
> tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
>
> rstride = kwargs.pop('rstride', 10)
>
> cstride = kwargs.pop('cstride', 10)
>
>
>
> color = kwargs.pop('color', 'b')
>
> color = np.array(colorConverter.to_rgba(color))
>
> cmap = kwargs.get('cmap', None)
>
>
>
> polys = []
>
> normals = []
>
> avgz = []
>
> for rs in np.arange(0, rows-1, rstride):
>
> for cs in np.arange(0, cols-1, cstride):
>
> ps = []
>
> corners = []
>
> for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
>
> ztop = a[rs][cs:min(cols, cs+cstride+1)]
>
> zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
> rs+rstride+1)]
>
> zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
> cs+cstride+1):]
>
> zbase = zbase[::-1]
>
> zright = ta[cs][rs:min(rows, rs+rstride+1):]
>
> zright = zright[::-1]
>
> corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
>
> z = np.concatenate((ztop, zleft, zbase, zright))
>
> ps.append(z)
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
> ps = zip(*ps)
>
> lastp = np.array([])
>
> ps2 = []
>
> avgzsum = 0.0
>
> for p in ps:
>
> if p != lastp:
>
> ps2.append(p)
>
> lastp = p
>
> avgzsum += p[2]
>
> polys.append(ps2)
>
> ##################################
>
> Begin of changes
>
> ##################################
>
> #avgz.append(avgzsum / len(ps2))
>
> avgz.append(colors[rs][cs])
>
> ##################################
>
> end of changes
>
> ##################################
>
>
>
> v1 = np.array(ps2[0]) - np.array(ps2[1])
>
> v2 = np.array(ps2[2]) - np.array(ps2[0])
>
> normals.append(np.cross(v1, v2))
>
>
>
> polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
>
> if cmap is not None:
>
> # polyc.set_array(np.array(colors))
>
> polyc.set_array(np.array(avgz))
>
> polyc.set_linewidth(0)
>
> else:
>
> colors = self._shade_colors(color, normals)
>
> polyc.set_facecolors(colors)
>
>
>
> self.add_collection(polyc)
>
> self.auto_scale_xyz(X, Y, Z, had_data)
>
>
>
> return polyc
>
>
>
>
>
> From: Mike Alger [mailto:ma...@ry...]
> Sent: November-25-09 8:42 PM
> To: mat...@li...
> Subject: Re: [Matplotlib-users] Color in 3d plots
>
>
>
> I have been looking at this for the past day and in am pretty sure I could
> replace the instance of polyc by the “cmap if statements” my colour array
> and I should be able to get close to what I want. However I am new to both
> python & mpl, and I am not entirely sure in how I would go about testing my
> hypothesis. Furthermore I am also relatively new to submitting fixes to
> open-source projects so I have lots of questions about how I would go about
> suggesting a modification.
>
>
>
> 1.) can I just modify the file in the
> C:\python26\Lib\site-packages\mpl-toolkits\mplot3d\axes3d.py file to do my
> tests?
>
> a. Also, where are these files usually kept in a linux environment ?
>
> b. What do I do with the. pyc files with the same name? are they
> re-complied automatically when I call the function externally?
>
> 2.) Is this capability already built in with the colour argument ? if so
> how do I properly call it?
>
> 3.) If I do make a modification should it be as a separate function with
> the additional variable or should I try to stuff the new capability into the
> old function
>
> 4.) is there a clean easy to follow tutorial for submitting changes via
> svn or can I rely on someone else to do the final commit?
>
>
>
> I have attached the function in question for reference to save others from
> digging down into their python directories
>
>
>
>
>
> Again thanks for taking your time to help me figure this out
>
>
>
> Mike Alger
>
>
>
> < Code>
>
> def plot_surface(self, X, Y, Z, *args, **kwargs):
>
> '''
>
> Create a surface plot.
>
>
>
> By default it will be colored in shades of a solid color,
>
> but it also supports color mapping by supplying the *cmap*
>
> argument.
>
>
>
> ========== ================================================
>
> Argument Description
>
> ========== ================================================
>
> *X*, *Y*, Data values as numpy.arrays
>
> *Z*
>
> *rstride* Array row stride (step size)
>
> *cstride* Array column stride (step size)
>
> *color* Color of the surface patches
>
> *cmap* A colormap for the surface patches.
>
> ========== ================================================
>
> '''
>
>
>
> had_data = self.has_data()
>
>
>
> rows, cols = Z.shape
>
> tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
>
> rstride = kwargs.pop('rstride', 10)
>
> cstride = kwargs.pop('cstride', 10)
>
>
>
> color = kwargs.pop('color', 'b')
>
> color = np.array(colorConverter.to_rgba(color))
>
> cmap = kwargs.get('cmap', None)
>
>
>
> polys = []
>
> normals = []
>
> avgz = []
>
> for rs in np.arange(0, rows-1, rstride):
>
> for cs in np.arange(0, cols-1, cstride):
>
> ps = []
>
> corners = []
>
> for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
>
> ztop = a[rs][cs:min(cols, cs+cstride+1)]
>
> zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
> rs+rstride+1)]
>
> zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
> cs+cstride+1):]
>
> zbase = zbase[::-1]
>
> zright = ta[cs][rs:min(rows, rs+rstride+1):]
>
> zright = zright[::-1]
>
> corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
>
> z = np.concatenate((ztop, zleft, zbase, zright))
>
> ps.append(z)
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
> ps = zip(*ps)
>
> lastp = np.array([])
>
> ps2 = []
>
> avgzsum = 0.0
>
> for p in ps:
>
> if p != lastp:
>
> ps2.append(p)
>
> lastp = p
>
> avgzsum += p[2]
>
> polys.append(ps2)
>
> avgz.append(avgzsum / len(ps2))
>
>
>
> v1 = np.array(ps2[0]) - np.array(ps2[1])
>
> v2 = np.array(ps2[2]) - np.array(ps2[0])
>
> normals.append(np.cross(v1, v2))
>
>
>
> polyc = art3d.Poly3DCollection(polys, *args, **kwargs) ## this is
> where a modification could be made to allow for a separate colour matrix
>
> if cmap is not None:
>
> polyc.set_array(np.array(avgz))
>
> polyc.set_linewidth(0)
>
> else:
>
> colors = self._shade_colors(color, normals)
>
> polyc.set_facecolors(colors)
>
>
>
> self.add_collection(polyc)
>
> self.auto_scale_xyz(X, Y, Z, had_data)
>
>
>
> return polyc
>
> </Code>
>
>
>
> From: Mike Alger [mailto:mik...@ng...]
> Sent: November-23-09 3:42 PM
> To: mat...@li...
> Subject: [Matplotlib-users] Color in 3d plots
>
>
>
> This may be a dumb question, however I have been scratching my head trying
> to figure out how to plot a 3 dimensional plot with with a colour map
> different from the elevation(Z) parameter.
>
>
>
> An example of this done in Matlab would be
>
>
>
> [X,Y,Z] = peaks(30);
>
> C=Z'% could be anything other than Z as long as it has the same dimensions
>
> surf(X,Y,Z,C)
>
>
>
> axis([-3 3 -3 3 -10 5])
>
>
>
>
>
> Is this possible with matplotlib '0.99.1'
>
>
>
> If so how do i go about doing this is there some sample code?
>
>
>
> Mike Alger, M.A.Sc
>
> ma...@ry...
>
>
>
> ------------------------------------------------------------------------------
> Join us December 9, 2009 for the Red Hat Virtual Experience,
> a free event focused on virtualization and cloud computing.
> Attend in-depth sessions from your desk. Your couch. Anywhere.
> http://p.sf.net/sfu/redhat-sfdev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
--
Reinier Heeres
Tel: +31 6 10852639
|
|
From: Michael (M. A. <ma...@ry...> - 2009-12-01 15:01:10
|
Sorry i was getting a bit frustrated by checking every day and seeing other problems getting solved relativly fast, again i understand its a big project, and i do appreciate your time, i hope i didn`t come off as a complete jerk. I figured since my suggested change would affect how others would interact with the existing function i should defer some interface design decisions to someone more familiar with the API and the interface philosophy before i would start suggesting my hacks to the code to get it to do what i want. again, i would love to build this functionality into the existing code, and it think it could be done via a good choice of kwargs parameters. Again i know how i can do this for my particular solution but how this should be done to minimize the effect on other users of the code i am not sure. thanks again for the response, i was going a bit crazy as i saw MPL as the perfect solution as it does just about everything i need except that one tiny variation in the surface command and i didn't want to rule it out its use in a user interface project i have work on in the coming months because of it. Mike ----- Original Message ----- From: John Hunter <jd...@gm...> Date: Monday, November 30, 2009 11:26 pm Subject: Re: [Matplotlib-users] Color in 3d plots To: Mike Alger <ma...@ry...> Cc: mat...@li..., Reinier Heeres <re...@he...> > On Mon, Nov 30, 2009 at 8:06 PM, Mike Alger <ma...@ry...> wrote: > > After a weekend of no replies I managed to figure a way out myself > > Hey Mike, sorry for the radio silence. mpl is a big project and no > one developer is equipped to answer questions about everything. We > currently have only one active developer (Reinier) working on the 3D > stuff. I've CCd him, and hopefully he can take a look at your patch. > > > > > 3.) If I do make a modification should it be as a separate > function with > > the additional variable or should I try to stuff the new capability > into the > > old function > > > > 4.) is there a clean easy to follow tutorial for submitting > changes via > > svn or can I rely on someone else to do the final commit? > > In general, if the new functionality is close to the old, we'd like to > see it incorporated into the existing API, perhaps with a new keyword > argument. We have some documentation on how to contribute to mpl at > http://matplotlib.sourceforge.net/devel/coding_guide.html; see also > the FAQ http://matplotlib.sourceforge.net/faq/howto_faq.html#contributing-howto > > > As this was “left to the reader as an exercise” I will leave the integration > > or improvement of this solution as an exercise to the next reader > > > > What I have done is basically cloned the plot surface function and replaced > > the avgz variable with a reference to the “colors” parameter i have > added to > > the function call. > > > > This code doesn’t center things perfectly with respect to the grid > (for > > some reason a 40x40 grid turns into 39x39 grid in the function) > again this > > is something else that could be improved, however I am happy with it > and a > > one pixel shift won’t be missed in my plots. I also have no real > clue as to > > what the following comments was about > > > > > > > > # The construction leaves the array with duplicate points, > > which > > > > # are removed here. > > > > > > > > but it is probably related to my non centered plots. > > > > What follows is the modified function : > > > > def plot_surface2(self, X, Y, Z, colors, *args, **kwargs): > > > What will be most helpful is an "svn diff", as explained in the coding > guide and FAQ linked above, with an example (included in the diff) > that shows the before and after behavior. That way even an naive > developer can appreciate the before and after changes and commit the > code if the original developer responsible for that part of the code > base is not available. The ideal situation is "apply this patch > generated from an svn diff and run example so_and_so.py to see the > plot with and without the patch". As explained in the FAQ, if you > don't get proper attention here on the mailing list, please post a bug > or patch on the sourceforge tracker so we don't lose it -- sometimes > our inattention is not due to lack of interest but to lack of time, > and a report filed on the tracker helps us not lose the thread. > > Thanks for the help! > > JDH |
|
From: Mike A. <ma...@ry...> - 2009-12-10 00:39:19
|
I had a hunch that was the case but wasn`t entirely sure, and to be honest
3d plot functions have always been a black box for me and i have never
really thought about what exactly gets plotted in 3d plot until now
Thanks again
Reinier i will take a look at the SVN A.S.A.P.
Mike
-----Original Message-----
From: Reinier Heeres [mailto:re...@he...]
Sent: December-09-09 7:17 PM
To: Mike Alger
Cc: mat...@li...
Subject: Re: [Matplotlib-users] Color in 3d plots
Hi Mike,
Sorry for the slow reply, but I put support for this in the
development version in SVN. It can also do a bit of shading to make
the surface look more structured.
Note that the fact that a 40x40 grid turns into 39x39 squares is
expected behavior: the code assumes the 40 points are the *edges* of
the patches. There are only 39 patches between 40 points.
Regards,
Reinier
On Tue, Dec 1, 2009 at 3:06 AM, Mike Alger <ma...@ry...> wrote:
> After a weekend of no replies I managed to figure a way out myself
>
> As this was left to the reader as an exercise I will leave the
integration
> or improvement of this solution as an exercise to the next reader
>
> What I have done is basically cloned the plot surface function and
replaced
> the avgz variable with a reference to the colors parameter i have added
to
> the function call.
>
> This code doesnt center things perfectly with respect to the grid (for
> some reason a 40x40 grid turns into 39x39 grid in the function) again this
> is something else that could be improved, however I am happy with it and a
> one pixel shift wont be missed in my plots. I also have no real clue as
to
> what the following comments was about
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
>
>
> but it is probably related to my non centered plots.
>
>
>
>
>
>
>
>
>
> What follows is the modified function :
>
>
>
>
>
>
>
> def plot_surface2(self, X, Y, Z, colors, *args, **kwargs):
>
> '''
>
> Create a surface plot.
>
>
>
> By default it will be colored in shades of a solid color,
>
> but it also supports color mapping by supplying the *cmap*
>
> argument.
>
>
>
> ========== ================================================
>
> Argument Description
>
> ========== ================================================
>
> *X*, *Y*, Data values as numpy.arrays
>
> *Z*
>
> *colors* an array the same size as z that contains a separate
> color data
>
> *rstride* Array row stride (step size)
>
> *cstride* Array column stride (step size)
>
> *color* Color of the surface patches
>
> *cmap* A colormap for the surface patches.
>
> ========== ================================================
>
> '''
>
>
>
> had_data = self.has_data()
>
>
>
> rows, cols = Z.shape
>
> tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
>
> rstride = kwargs.pop('rstride', 10)
>
> cstride = kwargs.pop('cstride', 10)
>
>
>
> color = kwargs.pop('color', 'b')
>
> color = np.array(colorConverter.to_rgba(color))
>
> cmap = kwargs.get('cmap', None)
>
>
>
> polys = []
>
> normals = []
>
> avgz = []
>
> for rs in np.arange(0, rows-1, rstride):
>
> for cs in np.arange(0, cols-1, cstride):
>
> ps = []
>
> corners = []
>
> for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
>
> ztop = a[rs][cs:min(cols, cs+cstride+1)]
>
> zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
> rs+rstride+1)]
>
> zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
> cs+cstride+1):]
>
> zbase = zbase[::-1]
>
> zright = ta[cs][rs:min(rows, rs+rstride+1):]
>
> zright = zright[::-1]
>
> corners.append([ztop[0], ztop[-1], zbase[0],
zbase[-1]])
>
> z = np.concatenate((ztop, zleft, zbase, zright))
>
> ps.append(z)
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
> ps = zip(*ps)
>
> lastp = np.array([])
>
> ps2 = []
>
> avgzsum = 0.0
>
> for p in ps:
>
> if p != lastp:
>
> ps2.append(p)
>
> lastp = p
>
> avgzsum += p[2]
>
> polys.append(ps2)
>
> ##################################
>
> Begin of changes
>
> ##################################
>
> #avgz.append(avgzsum / len(ps2))
>
> avgz.append(colors[rs][cs])
>
> ##################################
>
> end of changes
>
> ##################################
>
>
>
> v1 = np.array(ps2[0]) - np.array(ps2[1])
>
> v2 = np.array(ps2[2]) - np.array(ps2[0])
>
> normals.append(np.cross(v1, v2))
>
>
>
> polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
>
> if cmap is not None:
>
> # polyc.set_array(np.array(colors))
>
> polyc.set_array(np.array(avgz))
>
> polyc.set_linewidth(0)
>
> else:
>
> colors = self._shade_colors(color, normals)
>
> polyc.set_facecolors(colors)
>
>
>
> self.add_collection(polyc)
>
> self.auto_scale_xyz(X, Y, Z, had_data)
>
>
>
> return polyc
>
>
>
>
>
> From: Mike Alger [mailto:ma...@ry...]
> Sent: November-25-09 8:42 PM
> To: mat...@li...
> Subject: Re: [Matplotlib-users] Color in 3d plots
>
>
>
> I have been looking at this for the past day and in am pretty sure I could
> replace the instance of polyc by the cmap if statements my colour array
> and I should be able to get close to what I want. However I am new to both
> python & mpl, and I am not entirely sure in how I would go about testing
my
> hypothesis. Furthermore I am also relatively new to submitting fixes to
> open-source projects so I have lots of questions about how I would go
about
> suggesting a modification.
>
>
>
> 1.) can I just modify the file in the
> C:\python26\Lib\site-packages\mpl-toolkits\mplot3d\axes3d.py file to do my
> tests?
>
> a. Also, where are these files usually kept in a linux environment ?
>
> b. What do I do with the. pyc files with the same name? are they
> re-complied automatically when I call the function externally?
>
> 2.) Is this capability already built in with the colour argument ? if
so
> how do I properly call it?
>
> 3.) If I do make a modification should it be as a separate function
with
> the additional variable or should I try to stuff the new capability into
the
> old function
>
> 4.) is there a clean easy to follow tutorial for submitting changes via
> svn or can I rely on someone else to do the final commit?
>
>
>
> I have attached the function in question for reference to save others from
> digging down into their python directories
>
>
>
>
>
> Again thanks for taking your time to help me figure this out
>
>
>
> Mike Alger
>
>
>
> < Code>
>
> def plot_surface(self, X, Y, Z, *args, **kwargs):
>
> '''
>
> Create a surface plot.
>
>
>
> By default it will be colored in shades of a solid color,
>
> but it also supports color mapping by supplying the *cmap*
>
> argument.
>
>
>
> ========== ================================================
>
> Argument Description
>
> ========== ================================================
>
> *X*, *Y*, Data values as numpy.arrays
>
> *Z*
>
> *rstride* Array row stride (step size)
>
> *cstride* Array column stride (step size)
>
> *color* Color of the surface patches
>
> *cmap* A colormap for the surface patches.
>
> ========== ================================================
>
> '''
>
>
>
> had_data = self.has_data()
>
>
>
> rows, cols = Z.shape
>
> tX, tY, tZ = np.transpose(X), np.transpose(Y), np.transpose(Z)
>
> rstride = kwargs.pop('rstride', 10)
>
> cstride = kwargs.pop('cstride', 10)
>
>
>
> color = kwargs.pop('color', 'b')
>
> color = np.array(colorConverter.to_rgba(color))
>
> cmap = kwargs.get('cmap', None)
>
>
>
> polys = []
>
> normals = []
>
> avgz = []
>
> for rs in np.arange(0, rows-1, rstride):
>
> for cs in np.arange(0, cols-1, cstride):
>
> ps = []
>
> corners = []
>
> for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
>
> ztop = a[rs][cs:min(cols, cs+cstride+1)]
>
> zleft = ta[min(cols-1, cs+cstride)][rs:min(rows,
> rs+rstride+1)]
>
> zbase = a[min(rows-1, rs+rstride)][cs:min(cols,
> cs+cstride+1):]
>
> zbase = zbase[::-1]
>
> zright = ta[cs][rs:min(rows, rs+rstride+1):]
>
> zright = zright[::-1]
>
> corners.append([ztop[0], ztop[-1], zbase[0],
zbase[-1]])
>
> z = np.concatenate((ztop, zleft, zbase, zright))
>
> ps.append(z)
>
>
>
> # The construction leaves the array with duplicate points,
> which
>
> # are removed here.
>
> ps = zip(*ps)
>
> lastp = np.array([])
>
> ps2 = []
>
> avgzsum = 0.0
>
> for p in ps:
>
> if p != lastp:
>
> ps2.append(p)
>
> lastp = p
>
> avgzsum += p[2]
>
> polys.append(ps2)
>
> avgz.append(avgzsum / len(ps2))
>
>
>
> v1 = np.array(ps2[0]) - np.array(ps2[1])
>
> v2 = np.array(ps2[2]) - np.array(ps2[0])
>
> normals.append(np.cross(v1, v2))
>
>
>
> polyc = art3d.Poly3DCollection(polys, *args, **kwargs) ## this is
> where a modification could be made to allow for a separate colour matrix
>
> if cmap is not None:
>
> polyc.set_array(np.array(avgz))
>
> polyc.set_linewidth(0)
>
> else:
>
> colors = self._shade_colors(color, normals)
>
> polyc.set_facecolors(colors)
>
>
>
> self.add_collection(polyc)
>
> self.auto_scale_xyz(X, Y, Z, had_data)
>
>
>
> return polyc
>
> </Code>
>
>
>
> From: Mike Alger [mailto:mik...@ng...]
> Sent: November-23-09 3:42 PM
> To: mat...@li...
> Subject: [Matplotlib-users] Color in 3d plots
>
>
>
> This may be a dumb question, however I have been scratching my head trying
> to figure out how to plot a 3 dimensional plot with with a colour map
> different from the elevation(Z) parameter.
>
>
>
> An example of this done in Matlab would be
>
>
>
> [X,Y,Z] = peaks(30);
>
> C=Z'% could be anything other than Z as long as it has the same dimensions
>
> surf(X,Y,Z,C)
>
>
>
> axis([-3 3 -3 3 -10 5])
>
>
>
>
>
> Is this possible with matplotlib '0.99.1'
>
>
>
> If so how do i go about doing this is there some sample code?
>
>
>
> Mike Alger, M.A.Sc
>
> ma...@ry...
>
>
>
>
----------------------------------------------------------------------------
--
> Join us December 9, 2009 for the Red Hat Virtual Experience,
> a free event focused on virtualization and cloud computing.
> Attend in-depth sessions from your desk. Your couch. Anywhere.
> http://p.sf.net/sfu/redhat-sfdev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
--
Reinier Heeres
Tel: +31 6 10852639
|