Help with categorical scatter plot.

43 views (last 30 days)
Steph Varga
Steph Varga on 3 Jan 2016
Commented: Image Analyst on 27 Mar 2020
I'm trying to create a categorical scatter chart. The x-axis are the categories. Let's use 'Red' and 'Blue'. The y-axis are individual counts. Example: Blue has three values of [1,3,5]. Red has 4 values of [2,4,6,6,6.7]. I've read about plotting categorical data http://www.mathworks.com/help/matlab/matlab_prog/plot-categorical-data.html#zmw57dd0e19206 and it's not helping. I've attached a sample plot of what I'm trying to do - only the x-axis is supposed to be categorical. Any help would be appreciated.

Answers (2)

Image Analyst
Image Analyst on 3 Jan 2016
How about this:
% Plot Blue
Blue = [1,3,5]
x = ones(1, length(Blue));
plot(x, Blue, 'b*', 'MarkerSize', 10, 'LineWidth', 3);
grid on;
hold on;
% Plot Red
Red = [2, 4, 6, 6, 6.7];
x = 1.2 * ones(1, length(Red));
plot(x, Red, 'r*', 'MarkerSize', 10, 'LineWidth', 3);
% Set up axes.
xlim([0.5, 1.5]);
ylim([0, 8]);
ax = gca;
ax.XTick = [1, 1.2];
ax.XTickLabels = {'Blue','Red'};
grid on;
  3 Comments
James
James on 27 Mar 2020
Is there a way to make eack point under blue a different color?
Image Analyst
Image Analyst on 27 Mar 2020
James, yes you can find out which one is on top (highest) and then make that one blue, and all the other points will be under/below it and you can plot those with the default colors. By the way, check out my attached color order demo.
% Plot Blue with top most marker being blue and the ones under it being different colors.
Blue = [1,3,5]
x = ones(1, length(Blue));
% Get the index of the uppermost value
[v, indexOfTop] = max(Blue);
for k = 1 : length(x)
% Plot first one in blue, and the others using the other default colors.
if k == indexOfTop
plot(x(k), Blue(k), 'b*', 'MarkerSize', 10, 'LineWidth', 3);
else
plot(x(k), Blue(k), '*', 'MarkerSize', 10, 'LineWidth', 3);
end
grid on;
hold on;
end
% Plot Red
Red = [2, 4, 6, 6, 6.7];
x = 1.2 * ones(1, length(Red));
plot(x, Red, 'r*', 'MarkerSize', 10, 'LineWidth', 3);
% Set up axes.
xlim([0.5, 1.5]);
ylim([0, 8]);
ax = gca;
ax.XTick = [1, 1.2];
ax.XTickLabels = {'Blue','Red'};
grid on;

Sign in to comment.


Image Analyst
Image Analyst on 3 Jan 2016
How about this:
Blue = [1,3,5, nan, nan]
Red = [2, 4, 6, 6, 6.7];
bar([Blue, Red]);
hold on;
ax = gca;
ax.XTickLabels = {'Blue','Blue','Blue',' ',' ','Red','Red','Red','Red','Red'};
grid on;
  1 Comment
Steph Varga
Steph Varga on 3 Jan 2016
Awesome! Thanks so much, as well as for the super quick response.!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!