Change the background colour gradient.

Is there any way of changing the background colour of a MATLAB plot. I want to apply changes in the CTF Plot11.fig file and the idea I acquired was from a picture on wikipedia page "2013_Atmospheric_absorption_of_electromagnetic_waves".
https://en.wikipedia.org/wiki/Electromagnetic_spectrum

 采纳的回答

The axes color property can only be set to one solid color.
A workaround is to plot a patch with interpolated colors and to set the patch's size to match the axes' limits. By assigning a LimitsChangedFcn, you can automatically update the patch to match the axes limits any time the limits are changed.
% Create axes
fig = figure();
ax = axes(fig);
% Create interpolated patch
% You won't see the patch until the vertices are updated when the
% axes limits change. If you want to update the vertices right away,
% call updateAxesBackground(p) after creating the patch.
px = nan(1,4);
py = nan(1,4);
pc = [1 1 0 0];
p = patch('Faces', 1:4, ...
'Vertices', [px.',py.'], ...
'FaceVertexCData',pc.', ...
'FaceColor','interp', ...
'EdgeColor','none', ...
'Parent',ax);
% Assign a limitsChangeFunction so that when the axes limits are changed,
% the background patch updates its extents (here's the magic sauce)
ax.XAxis.LimitsChangedFcn = @(~,~)updateAxesBackground(p);
% Set colormap - here's where the colors come from
ax.Colormap = sky(256);
% Add other plots
hold on
area(ax,reshape(magic(10),[],1),'FaceColor',[.8 .8 .8])
function updateAxesBackground(p)
% update background patch to fill axes
% p is a patch handle.
if isgraphics(p) % skip if p no longer exists
ax = ancestor(p,'axes');
% Escape from recursion by ensuring limits don't change
originalLimits = [ax.XLim, ax.YLim];
returnLimits = onCleanup(@()set(ax,'XLim',originalLimits(1:2),'YLim',originalLimits(3:4)));
% Update vertices
px = ax.XLim([1 2 2 1]);
py = ax.YLim([2 2 1 1]);
p.Vertices = [px.',py.'];
end
end

5 个评论

Thank You for your help. The Output figure is not perfect like i wanted. I want to background color gradient to stop below the CTF curve.
That's not what the demo picture looks like from your question.
If you want the background color gradient to extend from the top of the axes to the line, you could merely cover the area below the line with white using area instead of plotting a line with plot().
area(data,'FaceColor','w')
The alternative is to define the vertices of the patch using the line's (x,y) data so the patch ends at the line. But that's significantly more work.
Plz elaborate your second option
Here's a demo.
In this version the patch is defined by the 6 coordinates shown in the drawing below and the (x,y) coordinates of your line.
This version is more complicated and less efficient than the simpler version in my original answer above.
% line data
y = reshape(magic(10),[],1);
x = 1:100; % Must be monotonically increasing
% Create axes
fig = figure();
ax = axes(fig);
% Create interpolated patch
% You won't see the patch until the vertices are updated when the
% axes limits change. If you want to update the vertices right away,
% call updateAxesBackground(p) after creating the patch.
ny = numel(y);
% px = [max(x,[],'all');min(x,[],'all');x(:)];
px = [x(end); nan(4,1); x(1); x(:)];
py = [nan(6,1); y(:)];
pc = [nan(6,1); y(:)];
p = patch('Faces', 1:ny+6, ...
'Vertices', [px, py], ...
'FaceVertexCData',pc, ...
'FaceColor','interp', ...
'EdgeColor','none', ...
'Parent',ax);
% Assign a limitsChangeFunction so that when the axes limits are changed,
% the background patch updates its extents (here's the magic sauce)
ax.XAxis.LimitsChangedFcn = @(~,~)updateAxesBackground(p);
% Set colormap - here's where the colors come from
ax.Colormap = sky(256);
% Add other plots
hold on
plot(ax,x,y,'k-','LineWidth',3)
function updateAxesBackground(p)
% update background patch to fill axes
% p is a patch handle.
if isgraphics(p) % skip if p no longer exists
ax = ancestor(p,'axes');
% Escape from recursion by ensuring limits don't change
originalLimits = [ax.XLim, ax.YLim];
returnLimits = onCleanup(@()set(ax,'XLim',originalLimits(1:2),'YLim',originalLimits(3:4)));
p.Vertices(4:5,1) = ax.XLim(1);
p.Vertices(2:3,1) = ax.XLim(2);
p.Vertices([1 2 5 6],2) = ax.YLim(1);
p.Vertices(3:4,2) = ax.YLim(2);
p.FaceVertexCData([1 2 5 6]) = ax.YLim(1);
p.FaceVertexCData(3:4) = ax.YLim(2);
end
end
Thank You. I also made some changes in the previous code to have better visualization.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Discrete Data Plots 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by