Problem in Visualising line plots and color coding the lines

4 次查看(过去 30 天)
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 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by