Using Fill between two curves semilog plot

19 vues (au cours des 30 derniers jours)
Emma
Emma le 21 Fév 2025
Commenté : Star Strider le 25 Fév 2025
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 commentaire
Walter Roberson
Walter Roberson le 22 Fév 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.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 22 Fév 2025
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 commentaires
Emma
Emma le 25 Fév 2025
This and what Walter pointed out about some of the values being negative fixed my issue. Thank you!
Star Strider
Star Strider le 25 Fév 2025
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by