Help with categorical scatter plot.

47 ビュー (過去 30 日間)
Steph Varga
Steph Varga 2016 年 1 月 3 日
コメント済み: Image Analyst 2020 年 3 月 27 日
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 日
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 件のコメント
James
James 2020 年 3 月 27 日
Is there a way to make eack point under blue a different color?
Image Analyst
Image Analyst 2020 年 3 月 27 日
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 日
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.!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by