Change the background colour gradient.

18 visualizaciones (últimos 30 días)
RAJEEV
RAJEEV el 25 de Jun. de 2024
Comentada: RAJEEV el 1 de Jul. de 2024
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

Respuesta aceptada

Adam Danz
Adam Danz el 25 de Jun. de 2024
Editada: Adam Danz el 26 de Jun. de 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 comentarios
Adam Danz
Adam Danz el 26 de Jun. de 2024
Editada: Adam Danz el 26 de Jun. de 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 el 1 de Jul. de 2024
Thank You. I also made some changes in the previous code to have better visualization.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by