MATLAB Answers

Insert Background Image Behind Subplots

17 views (last 30 days)
I'm looking for a way to plot an image behind a grid of subplots in MATLAB. I attempted to do so by plotting an image, and then plotting subplots but removing their backgrounds and axes. I have attached what the background image looks like and what the subplots in front look like.
Here is the stripped down code I used to attempt this setup. It results in only the subplot image file.
box = 20;
i = 10;
x_range = pw_responses(i).x_range; %x range for which to plot background image
y_range = pw_responses(i).y_range; %y range for which to plot background image
im = pref_mat(y_range,x_range); %this plotted image is in the background_im.png file
figure(i)
imagesc(im)
colormap(hsv)
set(gca,'XTick',[], 'YTick', [])
hold on
for j = 1:441 %the following subplots are in the subplots.png file
y_vals = surr_cell_resp(j,:); %response curve
subplot(box+1,box+1,j);
plot(x_vals,y_vals,'k')
set(gca,'visible','off')
set(gca,'XTick',[], 'YTick', [])
end
sgtitle(strcat('PW # = ',string(i)))

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Mar 2020
Hannah - How do I add a background image to my GUI or figure window? provides a good answer on how to add a background image to your figure. In your case, I suspect that you could do something like
box = 20;
i = 10;
x_range = pw_responses(i).x_range; %x range for which to plot background image
y_range = pw_responses(i).y_range; %y range for which to plot background image
im = pref_mat(y_range,x_range); %this plotted image is in the background_im.png file
hFig = figure(i);
hAxes = axes('Parent', hFig, 'units', 'normalized', 'position',[0 0 1 1]);
imagesc(im);
colormap(hsv);
set(hAxes, 'HandleVisibility', 'off', 'Visible','off');
for j = 1:441 %the following subplots are in the subplots.png file
y_vals = surr_cell_resp(j,:); %response curve
hSubPlot = subplot(box+1,box+1,j);
plot(x_vals,y_vals,'k')
set(hSubPlot, 'HandleVisibility', 'off', 'Visible','off', 'Color', 'none');
end

  1 Comment

Hannah Germaine
Hannah Germaine on 9 Apr 2020
Thank you Geoff, this worked perfectly! I simply modified the axes of the figure to:
hAxes = axes('Parent', hFig, 'units', 'normalized', 'position',[0.1300 0.1100 0.7750 0.8150]);

Sign in to comment.

More Answers (0)

Sign in to answer this question.