2d plot with colormap behind

3 次查看(过去 30 天)
Kaylie
Kaylie 2025-5-30
I am trying to make a colormap behind a 2d plot. It currently works for when the function which is represented by the line in decreasing but doesnt work for when it is increasing and vice versa for the unfolded/general case. I also sometimes get an artifact with some cases as it a function at the top of the plot where i have a blue section that should be yellow. The middle plot of the unfolded show that.
function y1_ss_param = steadystate2d_vsparam(params, params_values, expressions, variables, values, labels, tot0_value, threshold, lam_sig, sig)
figure;
original_var = variables;
c = {'c', 'm', 'r'};
for i = 1:length(params)
variables = original_var;
idx_removed = find(ismember(variables, params{i}));
if lam_sig == 1 && i == 3
sig_values = params_values(i, :) .* tot0_value;
idx_removedsig = find(ismember(variables, sig));
idx_removed = [idx_removed, idx_removedsig];
var = variables; val = values;
var(idx_removed) = []; val(idx_removed) = [];
y1_expr_subs = subs(expressions{1}, var, val);
for j = 1:length(params_values(i, :))
p_val = params_values(i, j);
s_val = sig_values(j);
y1_ss_param(i, j) = double(subs(y1_expr_subs, [params{i}, sig], [p_val, s_val]));
end
else
var = variables; val = values;
var(idx_removed) = []; val(idx_removed) = [];
y1result = subs(expressions{1}, var, val);
for j = 1:length(params_values(i, :))
y1_ss_param(i, j) = double(subs(y1result, params{i}, params_values(i, j)));
end
end
end
% Plotting
for i = 1:length(params)
subplot(length(params), 1, i)
x = params_values(i, :);
y = y1_ss_param(i, :);
if length(params)==3
Y_image = repmat(y, 1, length(x));
% Set threshold logic
if strcmp(labels{1}, 'Native [conc. (uM)]')
colormap(flipud(parula));
minthreshold = threshold;
maxthreshold = max(y);
else
colormap(parula);
minthreshold = min(y);
maxthreshold = threshold;
end
% Mask out-of-range values in Y_image
Y_image(Y_image < minthreshold | Y_image > maxthreshold) = NaN;
% Create y_display based on y range direction
if y(1) > y(end)
y_display = linspace(max(y) + 1, min(y) - 1, size(Y_image, 1));
else
y_display = linspace(min(y) - 1, max(y) + 1, size(Y_image, 1));
end
% Display image
imagesc(x, y_display, Y_image);
set(gca, 'YDir', 'normal');
clim([minthreshold, maxthreshold]);
colorbar;
hold on;
% Mask for axis limits
mask = y >= minthreshold & y <= maxthreshold;
x_visible = x(mask);
y_visible = y(mask);
if ~isempty(x_visible)
xmin = min(x_visible); xmax = max(x_visible);
ymin = min(y_visible); ymax = max(y_visible);
axis([xmin xmax ymin ymax]);
end
end
% Overlay curve
plot(x, y, c{i}, 'LineWidth', 2);
xlabel(labels{1 + i});
ylabel(labels{1});
set(gca, 'XScale', 'log');
% Formatting
ax = gca;
ax.YAxis.Exponent = 0;
ax.YTickLabelMode = 'auto';
ax.YMinorTick = 'on';
ax.MinorGridLineStyle = ':';
title([labels{1} ' vs ' labels{1 + i}]);
grid on;
hold off;
end
sgtitle(labels{end});
saveas(gcf, [labels{end} '.png']);
  2 个评论
Kaylie
Kaylie 2025-5-31
Matlab answers cut off my question part which is that, how could I fix and change my code to work when the function is increasing and how to fix the artifact of the current color map. Thank you!
William Rose
William Rose 2025-5-31
You provided a function. Please include a script (the simplest possible) that calls the function and demonstrates its use. If you need to attach some data to use as argument for the function, you can put those variables into a .mat file and post that too. Thank you.

请先登录,再进行评论。

回答(1 个)

William Rose
William Rose 2025-5-31
Here is a script that makes six curves that are similar to the six curves in your figures. It plots each curve with a colored background that corresponds to the y-values of the curve. I think that is what you wanted. Detailed comments in the script explain how it works. Modify the script to plot your data. The script makes the figure below. Compare it to the figures you posted.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by