フィルターのクリア

How to set for each point a different color

210 ビュー (過去 30 日間)
Tomas
Tomas 2014 年 3 月 1 日
コメント済み: Image Analyst 2018 年 7 月 22 日
Hello,
How do I set the color for points by plot,
I want to have for example 1000 points, each point is different color.
Thanks.

採用された回答

Image Analyst
Image Analyst 2014 年 3 月 1 日
You can use scatter and pass in the color for each point. See this demo:
% Demo to very color of scatterpoints depending on their location
clc;
clear
x = rand(1000,1);
y = rand(1000,1);
distances = ((x-.5).^2 + (y-0.5).^2).^0.5;
% Normalize - divide my sqrt(maxX^2 + maxY^2)
distances = distances / sqrt(.5^2 + .5^2);
[sortedDistances sortIndexes] = sort(distances);
% Arrange the data so that points close to the center
% use the blue end of the colormap, and points
% close to the edge use the red end of the colormap.
xs = x(sortIndexes);
ys = y(sortIndexes);
cmap = jet(length(x)); % Make 1000 colors.
scatter(xs, ys, 10, cmap, 'filled')
grid on;
title('Points where color depends on distance from center', ...
'FontSize', 30);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
  7 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 22 日
Brando is not the initial poster, and is asking about something slightly different that does not require the sorting and so on -- a way to be able to specify a color index for each point. Which is what already effectively happens if you pass a vector of values for color information.
Reflecting back, though, the code I posted does not truly treat the index vector as absolute indices into the color map. To do that with the colormap that is in effect, you would need:
numcolors = size(colormap(),1);
pointsize = 10;
scatter(x(:), y(:), pointsize, colorindex(:), 'filled');
caxis([1 numcolors])
assuming the index starts from 1 (which might not be the case for uint8 index information.)
Image Analyst
Image Analyst 2018 年 7 月 22 日
Sorting was not necessary in my code. I just did that because it make the colors progress with distance in a pretty way. You could omit the sorting and just have unique colors randomly all over the place. You can make up cmap however you want. You don't have to use jet() or one of the other built in functions, you could make it up according to your own formula. You could even use the color map editor if you want. I still don't see why it's annoying or overly complicated.

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

その他の回答 (0 件)

カテゴリ

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