Adding standard deviation as a bar to a scatter plot

조회 수: 38 (최근 30일)
Joseff Saunders
Joseff Saunders 2018년 12월 10일
답변: Jaswanth 2024년 7월 25일
Hello all, I'm plotting weather data and want to show the mean, standard deviation and min-max values in one plot. I've attached a photo of the ideal end result. How might I go about it using the data below as an example?
Mean_Temp_1 = [12]
Mean_Temp_2 = [15]
Mean_Temp_3 = [11]
Temp.jpg
SD_1 = [1.2]
SD_2 = [0.8]
SD_3 = [1.4]
Max_1 = [14]
Max_2 = [17]
Max_3 = [12]
Min_1 = [10]
Min_2 = [12]
Min_3 = [10]

채택된 답변

Jaswanth
Jaswanth 2024년 7월 25일
Hi Joseff,
To create a scatter plot in MATLAB that shows the mean, standard deviation, and min-max values for your weather data, you can use the errorbar function to add error bars representing the standard deviation and plot to add lines for the min and max values.
Please refer to the following example code demonstrating how to achieve this:
% Data
Mean_Temp = [12, 15, 11];
SD = [1.2, 0.8, 1.4];
Max_Temp = [14, 17, 12];
Min_Temp = [10, 12, 10];
% X-axis values
x = 1:3;
% Create the scatter plot with mean temperatures
figure;
scatter(x, Mean_Temp, 'filled');
hold on;
% Add error bars for standard deviation
errorbar(x, Mean_Temp, SD, 'LineStyle', 'none', 'Color', 'k', 'CapSize', 10);
% Plot min and max values as lines
for i = 1:length(x)
plot([x(i) x(i)], [Min_Temp(i), Max_Temp(i)], 'r-', 'LineWidth', 2);
end
% Customize the plot
xlabel('Data Points');
ylabel('Temperature (°C)');
title('Weather Data: Mean, Standard Deviation, and Min-Max Values');
legend('Mean Temperature', 'Standard Deviation', 'Min-Max Range', 'Location', 'Best');
grid on;
hold off;
Kindly refer to the following MathWorks documentation to know more about the functions discussed above:
Please refer to following MathWorks documentation on boxplot which may help in creating visualization similar to one shown the reference image you have posted: https://www.mathworks.com/help/stats/boxplot.html?searchHighlight=boxplot&s_tid=srchtitle_support_results_1_boxplot#bu180jd_vh
I hope the information provided above is helpful.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by