(plotting) How to change color and marker type according to data values
32 views (last 30 days)
Show older comments
Hi all. For context, I am a research student working on dark matter distribution in dwarf galaxies. I'm using Matlab to help visualize and present some of my data. I am trying to put together a plot from an excel file that has a fixed number of rows and columns.
Let's say this excel file has columns of mass, radius, alpha, and gamma. I want to plot mass against radius, but want the marker shape and color to be corresponding to parameters gamma and alpha. How would I do this? For example, gamma takes on values of either 0 or 1. If it's 0, I want that corresponding point from the plot of mass vs. radius to be a different color than if it were 1. The same goes for alpha (it's either 0 or 1 as well), except with a different marker shape. I know how to do a basic plot already but am not sure how to apply these extra conditions. Thanks very much.
1 Comment
Marta Salas
on 1 Apr 2014
you may find the solution http://www.mathworks.es/es/help/matlab/ref/linespec.html and http://www.mathworks.es/es/help/matlab/ref/linespec.html
Answers (1)
Image Analyst
on 1 Apr 2014
Edited: Image Analyst
on 1 Apr 2014
Try this with R2014a (untested):
t = readtable('yourExcelFile.xlsx');
radii = t.radii;
mass = t.mass;
alpha = t.alpha;
gamma = t.gamma;
gamma1 = gamma == 1; % Logical vector;
markerSize = 10;
% Where gamma = 1, make blue circles.
scatter(mass(gamma1), radii(gamma1), markerSize, 'MarkerFaceColor', 'b','MarkerType', 'o');
hold on;
% Where gamma = 0, make red crosses.
scatter(mass(~gamma1), radii(~gamma1), markerSize, 'MarkerFaceColor', 'r', 'MarkerType', '+');
Like I said, untested so it might need some tweaking.
1 Comment
Image Analyst
on 1 Apr 2014
If, unfortunately, you aren't using the latest version and don't have readtable(), then use xlsread() instead, but then you'll have to extract the arrays from cell arrays which isn't quite as easy as using tables.
See Also
Categories
Find more on Visual Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!