Problem in Visualising line plots and color coding the lines
1 次查看(过去 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;
0 个评论
采纳的回答
ProblemSolver
2023-7-10
编辑:ProblemSolver
2023-7-10
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!