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

 Réponse acceptée

Adam Danz
Adam Danz le 25 Juin 2024
Modifié(e) : Adam Danz le 26 Juin 2024
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 commentaires

RAJEEV
RAJEEV le 26 Juin 2024
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.
RAJEEV
RAJEEV le 26 Juin 2024
Plz elaborate your second option
Adam Danz
Adam Danz le 26 Juin 2024
Modifié(e) : Adam Danz le 26 Juin 2024
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
RAJEEV
RAJEEV le 1 Juil 2024
Thank You. I also made some changes in the previous code to have better visualization.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by