How to plot multiple lines whose colors change with density

16 ビュー (過去 30 日間)
GA
GA 2019 年 12 月 15 日
編集済み: Image Analyst 2021 年 1 月 11 日
I would like to make a chart like this...

回答 (1 件)

Image Analyst
Image Analyst 2019 年 12 月 15 日
I think you'll have to make a digital image to get that, rather than calling plot() a bunch of times to plot lines in different colors.
  2 件のコメント
Xin Shen
Xin Shen 2021 年 1 月 11 日
Sir! What do you mean digital image? Or any hints?
Image Analyst
Image Analyst 2021 年 1 月 11 日
編集済み: Image Analyst 2021 年 1 月 11 日
Like instead of just using plot() to plot some color, you'd create a 2-D matrix. Then you can make it like a histogram where you go along your curve and convert that (x,y) value into a (row, column) location and then add 1 to the image array at that location.
Like for 1 curve (untested)
rows = 1080
columns = 1920; % HDTV resolution
image2D = zeros(rows, columns); % Or whatever size you want.
x2 = rescale(x, 1, columns)
y2 = y * someScalingFactor;
for k = 1 : length(y)
row = x2(k);
col = y2(k);
image2D(row, col) = image2D(row, col) + 1;
end
someScalingFactor should be a value where the max value of y over ALL the curves gives you the number of rows in the image. Like if the some curves have max at 2, some others at 3, etc. but the max value of any y curve you have is 4, then you'd do
someScalingFactor = rows / 4;
So if you put in a y of 4, you'll get out 4 * (1080/4) = 1080 and the point will be at the top of the image.
Then once all ponts on the curve have been added to the array, do it for the next curve, and the next one, and the next on, and so one until all the curves have been added to the array. Then you can display the image with some colormap, like
maxValue = max(image2d(:));
imshow(image2D, [], 'Colormap', jet(maxValue));
axis xy;
colorbar;
Here is a more fleshed out demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Generate curves
numCurves = 300;
numElements = 600; % Number of points in our y vectors.
allY = zeros(numCurves, numElements);
x = 1 : numElements;
subplot(2, 1, 1);
for k = 1 : numCurves
fprintf('Generating curve #%d.\n', k);
thisPeriod = k*50;
thisY = cos(2 * pi * x / thisPeriod);
allY(k, :) = thisY;
plot(x, thisY, '-');
hold on;
drawnow;
end
grid on;
imageRows = 240; % whatever you want
imageColumns = 320; % whatever you want
image2D = zeros(imageRows, imageColumns); % Or whatever size you want.
col2 = rescale(x, 1, imageColumns);
y2 = rescale(allY, 1, imageRows);
for k = 1 : numCurves
fprintf('Adding in curve #%d.\n', k);
for xIndex = 1 : numElements
row = round(y2(k, xIndex));
col = round(col2(xIndex));
% fprintf('Assigning image value at (row, column) = (%d, %d) for curve #%d.\n', row, col, k);
image2D(row, col) = image2D(row, col) + 1;
end
end
maxValue = max(image2D(:))
subplot(2, 1, 2);
imshow(image2D, [], 'Colormap', jet(maxValue));
axis('on', 'xy');
colorbar;
g = gcf;
g.WindowState = 'maximized'
If you have trouble, start your own, new question and give the data for all the curves, or their formulas.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by