Reputation: 1382
I have a set of data in the x-y plane, and each point and each point has multiple "energies" associated. I can already plot the constant-energy contour of this surface at a chosen energy, but the problem is I also want to be able to change the colours along the contours according to the variation in another set of data.
Essentially I want to try and plot this graph but be able to vary the colour at any point on any contour according to a set of data that is unrelated to the contour's position (ignore the black dotted line):
My first attempt was to use the scatter function, the idea being that enough scatter points close together would look roughly like the solid lines of the contour, but with the freedom to set the colour of each scatter point independently. However, turns out this looks terrible, and it's slow because I essentially have to find the constant-energy surface myself.
My second attempt was to extrude the data into 3-D and use the isosurface function to create face and vertex data which can be plotted with patch, and patch allows the colour to be varied. I would then view this extruded surface end-on to get a contour plot. However, this gives an infinitesimally thin surface which disappears when viewed end on, as seen here where the view is not-quite end-on:
The 3D version of what I'm aiming for looks like https://i.sstatic.net/4UlpG.jpg
Does anyone know of either:
A method of plotting a 2-D contour which will allow me vary the colour independently and along any contour line,
A way to make the patch graphics objects use in the second picture thicker so that they aren't invisible when viewed end-on.
Thanks.
Upvotes: 0
Views: 351
Reputation: 1382
I found a solution, the key is to use ContourMatrix, which can be created using the contourc function.
This outputs a matrix containing a list of the x-y coordinates of the points making up the contours. Each contour can then be plotted using the line command. In order to vary the colour however, I used this trick with the surface command.
If I set the colour to vary simply with x position, my output now looks like this:
Upvotes: 1
Reputation: 5190
If, with your data set, you can use the contour3
function, you can set the colour of each line in the contour plot by menas of its handle.
In the following code, you can see how to set a random colour to each contour line of the peaks
function.
[x,y,z]=peaks
[c,h] = contour3(z);
for i=1:length(h)
set(h(i),'linewidth',2,'edgecolor',rand(3,1))
end
grid on
You can replace the rand
colour assignment, with the specific set of colour you wnat to use.
Peaks contour plot: Default colours
Peaks contour plot: Random colours
Hope this helps.
Upvotes: 1