Placing transparent rectangles on top of a plot

조회 수: 32 (최근 30일)
Emre
Emre 2024년 5월 14일
댓글: Voss 2024년 5월 15일
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));

채택된 답변

Voss
Voss 2024년 5월 14일
편집: Voss 2024년 5월 14일
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
Emre
Emre 2024년 5월 15일
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 2024년 5월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by