(plotting) How to change color and marker type according to data values

23 次查看(过去 30 天)
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 个)

Image Analyst
Image Analyst 2014-4-1
编辑:Image Analyst 2014-4-1
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 个评论
Image Analyst
Image Analyst 2014-4-1
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by