Assignment of colors in a scatter plot

14 ビュー (過去 30 日間)
Erdem Apaydin
Erdem Apaydin 2024 年 6 月 19 日
コメント済み: Erdem Apaydin 2024 年 7 月 11 日
I have a problem when using the colorbar in my scatter plot. I have a variable called flag, that can have the value 0, 1 or 2 and is used to plot the color of my scatter plot. Furthermore I defined a custom colormap with three colors which are assigned to each flag. The problem that arrises now is that the wrong colors are used in my scatter plot.
For example: Flag 1 is coded with green but the scatter shows it in blue (used for flag 0). I tried to solve it by setting the limits manually but it still has a problem with correctly assigning the colors.
c = colorbar;
c.Ticks = [0, 1, 2];
clim([0 2]);
Heres an example shown:
The first tile shows both data together and the two tiles at the bottom seperately. The colors in the third tile (bottom right) are shown correctly but in the first tile they are shown in a different color.
  1 件のコメント
DGM
DGM 2024 年 6 月 19 日
There are multiple ways to specify the colormapping of scatter plots, but you haven't shown us anything about how you're doing it.

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

採用された回答

SAI SRUJAN
SAI SRUJAN 2024 年 7 月 9 日
Hi Erdem,
I understand that you are facing an issue with assignment of colors in a scatter plot.
Please go through the following code sample of how you can correctly assign colors to your scatter plot based on a 'flag' variable and ensure the colorbar reflects these assignments properly.
x = rand(1, 100);
y = rand(1, 100);
% Sample flag data (0, 1, 2)
flag = randi([0, 2], 1, 100);
% Define a custom colormap with 3 colors
customColormap = [0 0 1; % Blue for flag 0
0 1 0; % Green for flag 1
1 0 0]; % Red for flag 2
scatter(x, y, 50, flag, 'filled');
colormap(customColormap);
caxis([0 2]);
c = colorbar;
c.Ticks = [0, 1, 2];
c.TickLabels = {'Flag 0', 'Flag 1', 'Flag 2'};
For a comprehensive understanding of the 'scatter' and 'colormap' functions in MATLAB, please refer to the following documentation.
I hope this helps!
  1 件のコメント
Erdem Apaydin
Erdem Apaydin 2024 年 7 月 11 日
Thanks a lot, that worked out!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by