Using Fill between two curves semilog plot

조회 수: 18 (최근 30일)
Emma
Emma 2025년 2월 21일
댓글: Star Strider 2025년 2월 25일
I'm trying to shade the standard deviation on a plot (between the red and blue lines I've plotted). But I can't get the fill function to work - any ideas? Am I just defining the bounds of the fill wrong? Does the fact that it's semilog change anything? Thank you!
%std_Zmod, mean_Zmod, and unique_freqs are all 36x1 doubles
% Calculate the bounds for the shaded region
upper_bound = mean_Zmod + std_Zmod; % Upper bound: mean + std
lower_bound = mean_Zmod - std_Zmod; % Lower bound: mean - std
figure;
hold on;
% Create shaded standard deviation region using fill function
fill([unique_freqs flipud(unique_freqs)], [upper_bound flipud(lower_bound)], ...
[0.6, 0.8, 0.6], 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Grey color
% Plot the bounds
plot(unique_freqs, lower_bound, 'r--'); % Lower bound in red dashed line
plot(unique_freqs, upper_bound, 'b--'); % Upper bound in blue dashed line
% Plot the mean line
plot(unique_freqs, mean_Zmod, 'o-', 'MarkerSize', 6, 'LineWidth', 2, 'Color', [0 0.3 0]); % Dark green
% Plot small electrode 1 kHz points
plot(1000, averageSmallElectrodeImpedance1, 'ko', 'MarkerSize', 8, 'LineWidth', 2, 'MarkerFaceColor', 'k');
plot(1000, averageSmallElectrodeImpedance2, 'ro', 'MarkerSize', 8, 'LineWidth', 2, 'MarkerFaceColor', 'r');
% Formatting
set(gca, 'XScale', 'log', 'YScale', 'log');
set(gca, 'FontSize', 14, 'FontWeight', 'bold', 'LineWidth', 1.5, 'XColor', 'k', 'YColor', 'k', 'Box', 'off');
xlabel('Frequency (Hz)', 'FontSize', 16, 'FontWeight', 'bold', 'Color', 'k');
ylabel('Impedance (Ω)', 'FontSize', 16, 'FontWeight', 'bold', 'Color', 'k');
ylim([1, 1e6]); % Set y-axis range from 1 Ω to 1 MΩ
grid on;
legend({'Standard Deviation', 'Gamry Potentiostat 2,000 \mum^2', 'HS-128B 2,000 \mum^2 (1 kHz)', 'HS-128S 10,000 \mum^2 (1 kHz)'}, ...
'Location', 'Best', 'FontSize', 14, 'TextColor', 'k');
set(gcf, 'Color', 'w');
hold off;
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 2월 22일
You would have problems with log scales if the data coordinates to be filled cross zero. fill() only works properly on log scale if all of the coordinates are positive.

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

채택된 답변

Star Strider
Star Strider 2025년 2월 22일
If you are using flipud, you will need to vertically concatenate the vectors (that are presumably column vectors), so instead of:
fill([unique_freqs flipud(unique_freqs)], [upper_bound flipud(lower_bound)], ...
[0.6, 0.8, 0.6], 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Grey color
that horizontally concatenates them, try this:
fill([unique_freqs; flipud(unique_freqs)], [upper_bound; flipud(lower_bound)], ...
[0.6, 0.8, 0.6], 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Grey color
note my use of ; to vertically concatenate the vectors.
That should work. (If it doesn’t, please post your code and attach/upload your data using the paperclip icon in the top toolbar.)
.
  댓글 수: 2
Emma
Emma 2025년 2월 25일
This and what Walter pointed out about some of the values being negative fixed my issue. Thank you!
Star Strider
Star Strider 2025년 2월 25일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by