Problem in Visualising line plots and color coding the lines

조회 수: 1 (최근 30일)
Mark
Mark 2023년 7월 10일
댓글: ProblemSolver 2023년 7월 10일
I have 100 sets of probability data which ranges from 1E-4 to 1E-6 that I would like to plot as line graphs. To enhance the visualization, I plan to take the logarithm of the probabilities and color code by the log of probability that it would occur. Additionally, I aim to sort the lines so that the highest probabilities are plotted on top for better visibility. However, the current MATLAB code I have attempted is not yielding the desired outcome. I kindly request your assistance in identifying any errors and providing guidance on visualizing the data more effectively.
% Set the range of random numbers
minValue = 1E-6;
maxValue = 1E-4;
% Generate 100 random numbers
total_probability = minValue + (maxValue - minValue) * rand(1, 100);
% Take the logarithm of the probabilities
log_probabilities = log(total_probability);
% Sort the probabilities in descending order
[sorted_probabilities, sorted_indices] = sort(log_probabilities, 'descend');
% Sort the original data accordingly
sorted_data = total_probability(sorted_indices);
% Plot the line plots with color coding based on log probabilities
figure;
hold on;
for i = 1:length(sorted_data)
color_value = sorted_probabilities(i) / max(sorted_probabilities);
plot(sorted_data(i), 'Color', [color_value 0 0]);
end
hold off;

채택된 답변

ProblemSolver
ProblemSolver 2023년 7월 10일
편집: ProblemSolver 2023년 7월 10일
Hello @Mark:
You are on the right track however you do these minor changes:
Instead of this:
% Set the range of random numbers
minValue = 1E-6;
maxValue = 1E-4;
% Generate 100 random numbers
total_probability = minValue + (maxValue - minValue) * rand(1, 100);
% Take the logarithm of the probabilities
log_probabilities = log(total_probability);
Use this:
% Take the logarithm of the probabilities
log_probabilities = log10(total_probability);
Instead of this:
% Plot the line plots with color coding based on log probabilities
figure;
hold on;
for i = 1:length(sorted_data)
color_value = sorted_probabilities(i) / max(sorted_probabilities);
plot(sorted_data(i), 'Color', [color_value 0 0]);
end
hold off;
Use this:
% Create a color map for the plot
cmap = jet(length(sorted_data));
% Plot the line plots with color coding based on log probabilities
figure;
hold on;
for i = 1:length(sorted_data)
plot([1, 2], [sorted_data(i), sorted_data(i)], 'Color', cmap(i, :));
end
hold off;
% Customize the plot
title('Line Plots of Logarithm of Probabilities');
xlabel('X');
ylabel('Probability');
colormap(cmap);
colorbar('Ticks', linspace(0, 1, length(sorted_data)), 'TickLabels', num2str(sorted_probabilities', '%0.2f'));
I hope this helps!
  댓글 수: 2
Mark
Mark 2023년 7월 10일
Thanks @ProblemSolver. It still doesn't show any line plots.
% Set the range of random numbers
minValue = 1E-6;
maxValue = 1E-4;
% Generate 100 random numbers
total_probability = minValue + (maxValue - minValue) * rand(1, 100);
% Take the logarithm of the probabilities
log_probabilities = log10(total_probability);
% Sort the probabilities in descending order
[sorted_probabilities, sorted_indices] = sort(log_probabilities, 'descend');
% Sort the original data accordingly
sorted_data = total_probability(sorted_indices);
% Plot the line plots with color coding based on log probabilities
figure;
hold on;
for i = 1:length(sorted_data)
color_value = (sorted_probabilities(i) - min(log_probabilities)) / (max(log_probabilities) - min(log_probabilities));
plot(sorted_data(i), 'Color', [color_value 0 0]);
end
hold off;
% Customize the plot
title('Line Plots of Logarithm of Probabilities');
xlabel('Index');
ylabel('Probability (log scale)');
colorbar;
colormap('jet');
ProblemSolver
ProblemSolver 2023년 7월 10일
Hello @Mark: Yeah I realized that it was missing something. I updated the code above, as well as I am providing the whole code here as well:
% Set the range of random numbers
minValue = 1E-6;
maxValue = 1E-4;
% Generate 100 random numbers
total_probability = minValue + (maxValue - minValue) * rand(1, 100);
% Take the logarithm of the probabilities
log_probabilities = log10(total_probability);
% Sort the probabilities in descending order
[sorted_probabilities, sorted_indices] = sort(log_probabilities, 'descend');
% Sort the original data accordingly
sorted_data = total_probability(sorted_indices);
% Create a color map for the plot
cmap = jet(length(sorted_data));
% Plot the line plots with color coding based on log probabilities
figure;
hold on;
for i = 1:length(sorted_data)
plot([1, 2], [sorted_data(i), sorted_data(i)], 'Color', cmap(i, :));
end
hold off;
% Customize the plot
title('Line Plots of Logarithm of Probabilities');
xlabel('X');
ylabel('Probability');
colormap(cmap);
colorbar('Ticks', linspace(0, 1, length(sorted_data)), 'TickLabels', num2str(sorted_probabilities', '%0.2f'));

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Orange에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by