Borrar filtros
Borrar filtros

Placing transparent rectangles on top of a plot

10 visualizaciones (últimos 30 días)
Emre
Emre el 14 de Mayo de 2024
Comentada: Voss el 15 de Mayo de 2024
Hey all,
I am trying to place several rectanges over a plot (wavelet coherence) but am struggling to do so.
I have been using the following code below, which has been creating the plots successfully but the rectangles do not seem to appear on the plots.
I would be so grateful for a helping hand! :)
Please let me know if there's anymore/example data I can provide.
wcoherence(data1,data2);
ax = gca;
final_onset = dataTable.onsetsWC(end);
final_duration = dataTable.durationsWC(end);
max_x_limit = final_onset + final_duration;
xlim([0, max_x_limit]);
for i = 1:height(dataTable)
onset = dataTable.onsetsWC(i);
duration = dataTable.durationsWC(i);
trialType = dataTable.Trialtype(i);
if strcmp(trialType, 'Chase')
barColor = [1, 0, 0, 1];
elseif strcmp(trialType, 'Follow')
barColor = [0, 1, 0, 1];
end
rectangle(ax, 'Position', [onset, 0, duration, 1], 'FaceColor', barColor, 'EdgeColor', 'none');
end
figure_title = sprintf('Dyad-%d-Channel-%d', p, j);
set(gcf, 'Name', figure_title);
save_path = 'C:\Users\emre.yavuz\Desktop\DataGLM\Waveletcoherence';
save_name = sprintf('Dyad_%d_Channel_%d.fig', p, j);
saveas(gcf, fullfile(save_path, save_name));

Respuesta aceptada

Voss
Voss el 14 de Mayo de 2024
Editada: Voss el 14 de Mayo de 2024
It's hard to say for sure what the problem is without the data, but one problem might be that the axes Y-Limits are outside where the rectangles are. The rectangles are all from y=0 to y=1, but the axes Y-Limits may be something else.
For example, with the made up data below, wcoherence creates an axes whose Y-Limits are approximately [-5.19, -1.44] (in spite of the fact that the y-tick labels suggest the Y-Limits are approximately [0.03, 0.3]).
To fix that problem, use the actual Y-Limits as the upper and lower limits of the rectangles. One way to do that is shown below. (I also included a FaceAlpha of 0.5 in the rectangles because otherwise they had no transparency.)
data1 = rand(100,1);
data2 = rand(100,1);
onsetsWC = [20;30;50;80];
durationsWC = [5;7.5;3;10];
Trialtype = {'Chase';'Follow';'Chase';'Follow'};
dataTable = table(onsetsWC,durationsWC,Trialtype);
wcoherence(data1,data2)
ax = gca;
ax.YLim % YLim is not what you might expect from looking at the plot
ans = 1x2
-5.1911 -1.4411
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
final_onset = dataTable.onsetsWC(end);
final_duration = dataTable.durationsWC(end);
max_x_limit = final_onset + final_duration;
xlim([0, max_x_limit]);
% use the actual axes YLim in defining the rectangles
y_lim = ax.YLim;
dy_lim = y_lim(2)-y_lim(1);
for i = 1:height(dataTable)
onset = dataTable.onsetsWC(i);
duration = dataTable.durationsWC(i);
trialType = dataTable.Trialtype(i);
if strcmp(trialType, 'Chase')
barColor = [1, 0, 0, 1];
elseif strcmp(trialType, 'Follow')
barColor = [0, 1, 0, 1];
end
rectangle(ax, 'Position', [onset, y_lim(1), duration, dy_lim], ...
'FaceColor', barColor, 'EdgeColor', 'none', 'FaceAlpha', 0.5);
end
  2 comentarios
Emre
Emre el 15 de Mayo de 2024
Thank you so so much for your help - that worked! :)
One thing - the FaceAlpha property wasn't recognised by the rectangle function, and so I had to change the transparency when I specified the red and green colours as follows:
wcoherence(data1, data2);
ax = gca;
final_onset = dataTable.onsetsWC(end);
final_duration = dataTable.durationsWC(end);
max_x_limit = final_onset + final_duration;
xlim([0, max_x_limit]);
y_lim = ax.YLim;
dy_lim = y_lim(2)-y_lim(1);
for i = 1:height(dataTable)
onset = dataTable.onsetsWC(i);
duration = dataTable.durationsWC(i);
trialType = dataTable.Trialtype(i);
if strcmp(trialType, 'Chase')
barColor = [1, 0, 0, 0.5]; %50 transparency
elseif strcmp(trialType, 'Follow')
barColor = [0, 1, 0, 0.5]; %50% transparency
end
rectangle(ax, 'Position', [onset, y_lim(1), duration, dy_lim], ...
'FaceColor', barColor, 'EdgeColor', 'black');
end
figure_title = sprintf('Dyad-%d-Channel-%d', p, j);
set(gcf, 'Name', figure_title);
save_path = 'C:\Users\emre.yavuz\Desktop\DataGLM\Waveletcoherence';
save_name = sprintf('Dyad_%d_Channel_%d.fig', p, j);
saveas(gcf, fullfile(save_path, save_name));
Voss
Voss el 15 de Mayo de 2024
You're welcome! Glad it's working.
"the FaceAlpha property wasn't recognised by the rectangle function, and so I had to change the transparency when I specified the red and green colours"
Strange, I had the opposite problem (which is why I changed it to use FaceAlpha). You can see here the rectangles are completely opaque (apparently not respecting the alpha as the 4th element of the color). I guess it's due to different versions of MATLAB.
data1 = rand(100,1);
data2 = rand(100,1);
onsetsWC = [20;30;50;80];
durationsWC = [5;7.5;3;10];
Trialtype = {'Chase';'Follow';'Chase';'Follow'};
dataTable = table(onsetsWC,durationsWC,Trialtype);
wcoherence(data1, data2);
ax = gca;
final_onset = dataTable.onsetsWC(end);
final_duration = dataTable.durationsWC(end);
max_x_limit = final_onset + final_duration;
xlim([0, max_x_limit]);
y_lim = ax.YLim;
dy_lim = y_lim(2)-y_lim(1);
for i = 1:height(dataTable)
onset = dataTable.onsetsWC(i);
duration = dataTable.durationsWC(i);
trialType = dataTable.Trialtype(i);
if strcmp(trialType, 'Chase')
barColor = [1, 0, 0, 0.5]; %50 transparency
elseif strcmp(trialType, 'Follow')
barColor = [0, 1, 0, 0.5]; %50% transparency
end
rectangle(ax, 'Position', [onset, y_lim(1), duration, dy_lim], ...
'FaceColor', barColor, 'EdgeColor', 'black');
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by