Matlab 2-D color plot without imagesc

Greetings,
I have a 2-D data structured as (x,y,z) where x and y are the coordinates vectors and z is the data vector I am interested in (in my case z is a particular component of the electromagnetic field).
I would like to be able to have a 2-D plot where z values are addressed as colors instead of height so that the plot is not 3-D. Therefore, the appropriate command is not "plot3" and the data is scattered at first so I interpolate it to have a uniform grid. My aim is to have a color plot just like "imagesc" does but since I do not have an array "imagesc" does not work in my case. Which function should I use and is there an elementary solution?
Thanks in advance,
Ongun Arisev
P.S: My working code can be found on the following address: http://pastebin.com/K7gH9C5z

Respuestas (2)

Mike Garrity
Mike Garrity el 8 de Oct. de 2015
What you have is 2D scatter data with colors. Let's make up a simple example and look at your options:
rng default
x = randn(1,1000);
y = randn(1,1000);
c = cos(x) .* cos(y) + rand(1,1000)/10;
The simplest option is to use the scatter command. This will just give you a colored point at each location with no interpolation between them.
scatter(x,y,50,c,'filled','MarkerFaceAlpha',.75)
Your next option, as Image Analyst said, is to interpolate it onto a uniform grid. There are a lot of ways to do this in MATLAB. One good one is scatteredInterpolant.
F = scatteredInterpolant(x',y',c');
[xq,yq] = meshgrid(linspace(-3,3,100));
cq = F(xq,yq);
h = pcolor(xq,yq,cq);
h.EdgeColor = 'none';
As you can see, it's a bit wonky in the areas where it has extrapolated past the edges of the data you gave it. You can tell it not to extrapolate.
And the third option to consider is to create a triangulation.
dt = delaunayTriangulation(x',y');
h = trisurf(dt.ConnectivityList,x',y',zeros(1000,1),c');
h.FaceColor = 'interp';
h.EdgeColor = 'none';
view(2)
There are a couple of other options, but one of those will probably be your best bet. I would probably need to know more details before I could pick the best of those three.
Does that help?

7 comentarios

Ongun
Ongun el 8 de Oct. de 2015
Editada: Ongun el 8 de Oct. de 2015
Thanks Mike Garrity, I think I can make us of this information. One more thing I already have the array c in your notation coming from COMSOL so I will not write a function for it. Will it work in this case too? By the way can I put a colorbar next to the graph?
Ongun
Ongun el 8 de Oct. de 2015
Although I get the plot in my case I also get the following error:
Error using scatter (line 44) There is no MarkerFaceAlpha property on the Scatter class.
Image Analyst
Image Analyst el 8 de Oct. de 2015
It could be a newer option. What version/release do you have? Can you upgrade? Otherwise, just remove that option.
Mike Garrity
Mike Garrity el 8 de Oct. de 2015
Sorry, MarkerFaceAlpha was just added in R2015b. I should have probably left that out.
Ongun
Ongun el 9 de Oct. de 2015
Editada: Ongun el 9 de Oct. de 2015
I have R2014b and it throws another error when it is left out. The error is: Subscript indices must either be real positive integers or logicals.
Error in comsoldata (line 12) scatter(x,y,50,Fscatter,'filled')
When I have the option you mentioned provided it plots a graph with a colormap; however break the loop and throws the error I mentioned in my previous comment.
Walter Roberson
Walter Roberson el 9 de Oct. de 2015
'filled' is a new option as well. Leave it out.
Mike Garrity
Mike Garrity el 24 de Feb. de 2016
I cleaned this answer up a bit, and turned it into a post on the MATLAB Graphics blog.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 8 de Oct. de 2015

0 votos

How about using imshow()?

3 comentarios

Ongun
Ongun el 8 de Oct. de 2015
Well thanks for the suggestion how should I use this function?
Image Analyst
Image Analyst el 8 de Oct. de 2015
Interpolate it to a uniform grid, like you said. Then call it. You can use scatteredInterpolant() then, with the image you get, call imshow(). I don't have any demo for that process but I'm sure a smart engineer like you can figure it out from the examples in the help. Post your code if you need more help with it.
Ongun
Ongun el 9 de Oct. de 2015
Okay thanks for the tips, I will post again if needed.

Iniciar sesión para comentar.

Categorías

Más información sobre Color and Styling en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Oct. de 2015

Comentada:

el 24 de Feb. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by