Clear Filters
Clear Filters

Problem in Visualising line plots and color coding the lines

1 view (last 30 days)
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;

Accepted Answer

ProblemSolver
ProblemSolver on 10 Jul 2023
Edited: ProblemSolver on 10 Jul 2023
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 Comments
Mark
Mark on 10 Jul 2023
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 on 10 Jul 2023
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'));

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!