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개)

Image Analyst
Image Analyst 2016년 1월 3일

1 개 추천

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

Steph Varga
Steph Varga 2016년 1월 3일
Even better!
James
James 2020년 3월 27일
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;

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2016년 1월 3일

0 개 추천

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

Steph Varga
Steph Varga 2016년 1월 3일
Awesome! Thanks so much, as well as for the super quick response.!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기

질문:

2016년 1월 3일

댓글:

2020년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by