Help with categorical scatter plot.

53 Ansichten (letzte 30 Tage)
Steph Varga
Steph Varga am 3 Jan. 2016
Kommentiert: Image Analyst am 27 Mär. 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.

Antworten (2)

Image Analyst
Image Analyst am 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 Kommentare
James
James am 27 Mär. 2020
Is there a way to make eack point under blue a different color?
Image Analyst
Image Analyst am 27 Mär. 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;

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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 Kommentar
Steph Varga
Steph Varga am 3 Jan. 2016
Awesome! Thanks so much, as well as for the super quick response.!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by