Overlapping axes in GUI when plotting boxplot in MATLAB - matlab

I am creating a GUI in MATLAB using GUIDE. I have several axes, and in one of them I want to draw a boxplot. My problem is that after drawing the boxplot, the size of the axes changes, and it overlaps with some of my other figures.
To replicate this problem, create a .fig file using GUIDE containing two axes: axes1 and axes2, as shown in the figure: .
Then, in the OpeningFcn, add the following lines:
Z = normrnd(1,3,[100,1]);
plot(handles.axes1, Z);
boxplot(handles.axes2,Z)
Then lauch the GUI. I see the following:
As you can see, the two axes overlap. I've tried changing the properties of the box plot, but with no luck.
I use MATLAB 7.10 (R2010a) and Kubuntu 12.10.

It appears that boxplot makes the axes grow wider, not sure why. In any case, saving the axes position right before plotting and resetting it right after seems to work for me:
Z = normrnd(1,3,[100,1]);
plot(handles.axes1, Z);
pos = get(handles.axes2, 'position');
boxplot(handles.axes2,Z);
set(handles.axes2, 'position', pos);
Cheers,
Giuseppe

Related

How to Control Relative Size of Figures with Colorbar in Matlab?

I am trying to line up a plot of a signal with an image where the image has a colorbar. The colorbar causes the axes to be offset horizontally.
My intuitive approach would be to fix the size of figures to something, like in Gnuplot with papersize. However, not sure which would be the best fit here.
To Adjust Scaling to Square in Full Screen Mode of Matlab?
I want to maintain the relations between the two figures. I cannot use squareform in the first figure for some reason, while I can in the latter figure.
Code
figure
ax2=subplot(2,2,2);
plot(mat2gray(pdist(data, 'correlation')));
title('Corr pdist');
cbar2 = colorbar(ax2);
xlim([0 size(mat2gray(pdist(data, 'correlation')),2)]);
set(cbar2, 'Visible', 'off');
ax4=subplot(2,2,4);
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') );
colormap('parula'); colorbar;
title('Square Corr pdist');
Wrong Scaling in Output when Full Screen Mode of Matlab where you see the colorbar method is not sufficient for holding relations as proposed in the answer here about How to Control Relative Size of Figures with Colorbar in Matlab?
Right Scaling in Output when Default View
How can you maintain the square view of figures in the Full Screen Mode of Matlab?
I would simply create a colorbar for the top axes as well and set the visibility to off.
figure;
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
cbar1 = colorbar(ax1);
cbar2 = colorbar(ax2);
set(cbar1, 'Visible', 'off')
The benefit here is that you will get consistent behavior when resizing the figures, etc. because the size and position of the two axes will be rendered in the same way.
The other thing you will need to remember is to keep the axes the same in all aspects. So for example if you have an image in the bottom axes (using imshow), MATLAB by default sets the axes to a square. To get your top plot to also be square you will need to use axis square. Then they will continue to line up.
axis(ax1, 'square')

Linear and Non-linear axis in Matlab

I'm the MatLab newbie and I need some help to create a linear and non-linear axis in one chart.
I need to make chart with 2 different X-axes. One X-axis displays 1000/T at the bottom and the second X-axis displays a T at the top of the chart.
Example figure:
Do you have any idea how to solve this problem in MatLab?
Thanks.
This can be done by simply creating a second axes object at the same place as the first. Let's first create some data:
x1 = 1:0.1:3.5;
x2 = 1./x1;
y = (0.5*(x1-2)).^3;
Now we can create a normal plot with the first axes, and get the axes handle:
plot(x1,y,'-r');
ax(1) = gca;
Then we create the second axes object, at the same position as the first, and make the color none so it is transparent and the plot from below is still visible. As this adds a second Y axis too, we simply remove the Y ticks of the second axis.
ax(2) = axes('Position',ax(1).Position,'XAxisLocation','top','Color','none');
set(ax(2),'YTick',[]);
Now lets just format the second X axis as we like. Let's set the limits to the minimum and maximum of the x2 vector, and make it logarithmic:
set(ax(2),'XLim',[min(x2),max(x2)]);
set(ax(2),'XScale','log');
Now we still have the problem that the XTicks of ax(1) are also displayed at the top, and the XTicks of ax(2) are displayed at the bottom. This can be fixed by removing the box around the existing axes and creating a third axis without any ticks but with a box.
box(ax(1),'off');
box(ax(2),'off');
ax(3) = axes('Position',ax(1).Position,'XTick',[],'YTick',[],'Box','on','Color','none');
Now finally we can link the axes to be able to zoom correctly
linkaxes(ax);
And that should be it...
There is documentation for having a graph with two y-axes on the Mathworks website . .
http://de.mathworks.com/help/matlab/creating_plots/plotting-with-two-y-axes.html
It should be trivial to covert the concepts to the x-axis.

Heatmap plots extra axes

I'm using the heatmap function in Matlab to plot some maps, the maps themselves are fine but the program seems to be adding extra borders and axes onto the figures, no idea why this is happening!
My code is:
figure(1)
hFig = figure(1);
set(gcf,'PaperPositionMode','auto')
set(hFig,'Position',[1000 1000 900 800])
colormap('hot');
imagesc(data)
xlabel('X({\mu}m)')
ylabel('Y({\mu}m)')
Here is an image of what I mean by extra axes:
Thanks!
Edit1: Here is the image after the first proposed fix:
Remove the xlabel and ylabel from the last lines of your code. Since you have already used the set function you can integrate them directly by doing
imagesc(data);
colomap('hot');
set(gca,'Xtick',[0:5:50],'XtickLabel',[0:5:50]);
set(gca,'Ytick',[0:5:50],'YtickLabel',[0:5:50]);
colorbar('YtickLabel',{'1000','900','800'});

Holding multiple axes' plots in Matlab GUI:

I'm currently developing a GUI (programatically. No GUIDE has been used) for a project and I need to place 11 axes on the same GUI. I'm using the axes command to get the handles of the 11 controls:
h.AXES_ALL(1)=axes('parent',h.fig,'position',[L1 T W H]);
h.AXES_ALL(2)=axes('parent',h.fig,'position',[L2 T W H]);
h.AXES_ALL(3)=axes('parent',h.fig,'position',[L3 T W H]);
...
They all have the same dimensions and I'm using the for instruction to plot the data:
for i=1:11
set(h.PLOT(i),'parent',h.AXES_ALL(i),'XData',x_data,'YData',y_data);
end
But the problem is that the last plot (the 11th) is the one that is shown on the axes control (the 11th) and all the other axes are empty. My objective is to plot 11 curves on 11 different axes controls. They aren't located in the same position, just for the record.
THanks in advance!
Charlie
You said in your comment that you start with a single axes handle:
ha = axes;
And you try to create two plots with the same parent axes, but it does not work as you intended:
>> h.PLOT(1:2) = plot(ha,0,0)
h.PLOT =
195.0035 195.0035
That just replicated the same plot series handle. So, when you go to set the plot data and parent axes for each plot, you are just moving the plot from axes to axes, updating the data while you go.
Use the plot command in a loop, using the appropriate axes handle for each plot:
for ip=1:11,
h.PLOT_ALL(ip) = plot(h.AXES_ALL(ip),...);
end
Then when you update the plot's XData and YData as you want to do, you do not have to change the parent axes.

How to plot graphs above each other in Matlab?

I want to plot two (or more) graphs in Matlab. I want them to be aligned one above the second one above the third and so on. However subplot is not the case for me. I also don't want them both to be on the same graph in different colors, just to be one above the other. Is it possible? I'm searching for such a command but without any success.
Thanks in advance for hints!
If what you mean by "one above the other" is one "on top of another" then I think what You need to use is the axes command. Having said that, in order to have your plot be readable, I think more than two scales in the same plot makes it pretty hard to read.
This Matlab's help page shows you how to do it, the gist of it is shown in the following script:
y2 = 1000.*rand(100,1);
x2 = 0:99;
% Plot the first data set
hl1 = line(x1,y1,'Color','r');
% Get the axes and configure it
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
%Create the new axes
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
% Plot the second data set with the new axes
hl2 = line(x2,y2,'Color','k','Parent',ax2);
The above script creates the following plot with one set of axes in red and another in black.
Hope this helps.