Help with categorical scatter plot.

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.

回答(2 个)

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 个评论

Is there a way to make eack point under blue a different color?
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;

请先登录,再进行评论。

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 个评论

Awesome! Thanks so much, as well as for the super quick response.!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by