Using Fill between two curves semilog plot

21 次查看(过去 30 天)
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!

请先登录,再进行评论。

更多回答(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