Display multiple axes on image plot - matlab

I have a large plot showing an image. Now, I want to diplay several small bar plots on top of that image, each having its own axes and positioned on a specified position on the image.
I already tried to just append more axes (with transparent background color) to the same figure. This basically works, but If I now zoom in or pan around the background image, the small axes stay on the same position relative to the figure, so they lose the relation to their position on the background image.
Does Matlab offer a better solution?

I recommend drawing the bars using patch command.
For example:
plot(rand(10));
hold on;
patch([1;1;2;2],[1;2;2;1],'r')

Related

How to draw rectangle around tiled object in a MATLAB plot?

I created a tiledlayout(4,3)-object and I want to emphasize the 8th tile by a surrounding line or changing the backgroundcolor (not of the graph itself, but just the greyspace behind the graph, labels, axes etc.).
I could not find anything that changes the backgroundcolor of just one tile, so I tried to draw a rectangle around the tile and color it.
I executed the code below and expected to get a red line around the OuterPosition- or the TightInset-position, but nothing happened. The code created the tiles without any error-codes and there was no red rectangle around the 8th tile.
Can anyone tell me how to color the background of a tile or get a rectangle around it?
Thank you very much!
ax = nexttile(8)
lines_around_tile = get(ax, 'OuterPosition'); %'TightInset')
rectangle('Position', lines_around_tile, 'EdgeColor', 'r', 'LineWidth',10)
I got this result.
Result
To draw a rectangle, run the code and in matlab figure (with all the tiles) go to view -> Plot Edit Toolbar. It generates a toolbar where you can find lines, rectangles, circles...
Then insert your rectangle, right click on it, Property Inspector. In this window you can change the facecolor, linecolor, linewidth...

Matlab: Put an image ON TOP of a plot, after the data has been plotted

I have searched the internet for a solution to the question above but have had no luck up to now. I have been producing a number of 2D plots where the origin of (0,0 point) is represented by an image. I have made these by plotting the data on an image, where the image is all white apart from the desired symbol at the center point (so I can reshape it as needed). I then move the axis so they cross at the center point. This all works fine, but I now have to create a number of plots using ‘fill’ to place two shaded areas on the plot that overlap in the center. This causes the symbol to be difficult to see even using ‘alpha’.
I therefore have two options to get the desired effect, both requiring me to put an image on top of the figure after the data is plotted. These options are:
1) I place the image on top of the plot and apply alpha to it (May not look very good as it will mute the plot).
2) The better option would be to crop the image around the symbol and then position it on top of the plot so the image center is at the origin (I have no idea how to position the image this way).
Both methods need the image to be placed on top of the plot after the data is plotted. I have tried 'hold on' and calling 'figure(imagesc(Image))' neither work. I have Matlab 2012b but no toolboxes (so cannot use subimage etc.)
Thanks for any help you can give
You can set the transparency of individual pixels of an image using the 'AlphaData' option. So, you can plot the overlay as follows:
% plot your data normally
...
hold on
% assuming the overlay is a 11x11 image
image(-5:5,-5:5,image_matrix,'AlphaData',alpha_matrix);
image_matrix would obviously be the matrix with your image data stored in it, while alpha_matrix would be a matrix of the same size as image_matrix. Every pixel you want to show would have a value of 1, every pixel you want to hide (the white pixels) would be 0.

How do you fix the size of an axes graphic object?

I'm creating a simple GUI in MATLAB and I'm trying to create a figure that contains a 2d plot on an axes, all is working well, but I have a rough time trying to figure out how to fix the position of the axes/plot.
The axes seems to scale with the figure window size. If I maximize the figure on my screen, the axes is maximized within, etc. What I want to accomplish is to have a row of buttons beneath the plot on the same figure as the plot, basically an area of 50 (ish) pixels tall that the plot does not encroach on. I know how to do this in HTML, but I can't figure out a good way in MATLAB.
Any alternative approaches would also be greatly appreciated.
Change the units of the axes to anything other than 'normalized', like 'pixels'. Then it won't automatically resize with the figure. From this documentation page:
When you create a graph, MATLAB® creates an axes to display the graph. The axes is sized to fit in the figure and automatically resizes as you resize the figure. MATLAB applies the automatic resize behavior only when the axes Units property is set to normalized (the default).
Use set(gca,'Position',pos) where pos = [x y w h] to set the position and size of the axes in the units you chose.
See this answer for an example and a function for holding an axis size once you have it in place.

impixelinfo with value transformation

I want to display an image and allow mouse over to show pixel values- as in impixelinfo function.
The thing is- I show the image on an inverse scale (1./image) but want the shown values to be the original ones.
Is there some way to do it?
A possible workaround - puts your original image over the top as a transparent layer, leaving the visible image (1./image) for the user and an invisible image which impixelinfo takes its data from.
imshow(1./image);
hold on
h = imshow(image);
set(h, 'AlphaData', zeros(size(image)));
impixelinfo

Draw Matlab graphs with frame, ticks, on top of graph lines

Consider something like
figure
plot(sin(0:0.01:pi))
axis tight
set(gca,'box','on','ticklength',[0.02 0.05])
then export the graph to PDF or whatever. The lines of the graph are on top of the tick labels and the axes. (Furthermore, the lines of the axes don't meet correctly, but that's another story.)
Is there a way (that can be automated) to have the axes drawn on top?
Try:
set(gca, 'Layer','top')
according to the documentation page:
Layer
{bottom} | top
Draw axis lines below or above graphics objects. Determines whether
to draw axis lines and tick marks on
top or below axes children objects for
any 2-D view (for example, when you
are looking along the x-, y-, or
z-axis). Use this property to place
grid lines and tick marks on top of
images.
and to visually see the effect (zoomed in 1200%), I save the figure as a PDF file:
Default (Layer=bottom):
with Layer=top: