Using Fill between two curves semilog plot
Show older comments
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 Comment
Walter Roberson
on 22 Feb 2025
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.
Accepted Answer
More Answers (0)
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!