I am using a plotyy in matlab to plot two data set in a same figure. The left and right axis are of the different range. But I just find that on the right axis, there seems to be two different set of scale shown. I think one of them is really from the left axis.
t=-1:0.02:1;
y=sin(t);
y1=2*sech(t);
[AX, H] =plotyy(t, y, t, y1);
ylim(AX(2), [0 3.25]);
set(AX(2), 'YTickMode', 'auto')
After searching that online, I found that to turn off the box will solve the problem too. But the problem is to turn the box off will case the top horizontal line gone also. Is that anyway to remove the extra scale and keep the frame? Thanks.
It is possible and not terribly difficult, here's an illustrative example figure based on your test code
What I've done is to add a third axis (in this case I accomplished this by taking a shortcut - I called plotyy twice resulting in an extra blue line in the first axis and an extra second axis with a green line).
The idea is to turn the bounding box off for both the first and second axes, and then to turn it on for the third. That results in the top axis giving you the left vertical axis, the second the right vertical axis, and the third the top horizontal axis.
I don't think there's simple a way to do what you want. If you turn off the box (to get rid of the blue ticks on the right hand side) then the top horizontal line will disappear:
set(AX(1), 'Box','off')
If you want you could re-draw the line back in with:
line([-1, 1], [1, 1])
Or more generally:
lims = get(AX(1),{'XLim','YLim'});
line(lims{1}, lims{2}([2 2]))
Related
I wanted to add the mean of the Young's modulus to my box plot. It works perfectly fine with a vertical box plot, but I can’t get it to work with a horizontal box plot... It looks like MATLAB is doing something, but not displaying anything except the box plot.
I tried to fix the axis, but that doesn’t work either. It looks like whenever the box plot is rotated everything that worked before gets thrown away.
%Boxplot all tensile strength tests
figure(f1)
subplot(2,1,2)
hold on
xlabel('Youngs Modulus [MPa]') ;
ylabel('Samples:') ;
boxplot(E,k,'orientation','vertical') ;
plot(E_mean,'ok');
hold off
plot(E_mean)
is the same as
plot(1:length(E_mean), E_mean)
That is, you plot values as the y coordinate against their index as the x coordinate.
When making a horizontal box plot, the function switches the coordinates to plot. That is, it plots the index as the y coordinate, and the values as the x coordinate. Thus, you should do:
plot(E_mean, 1:length(E_mean))
I know that Matlab sometimes isn't the best tool to create "fancy" plots but since my university requires it I dont have much choice.
I want to move the Y-Axis and the "arrow"-Annotation of the following example to x=0.
X = -pi/2 : 0.001 : pi/2;
Y = cos(X).^2;
plot(X, Y,'Color',[0,0,1]);
ylim([0 1.2]);
set(gca, 'YTick',[1.2],'yticklabel',{'{\color[rgb]{0,0,1}X(f)}'});
set(gca, 'XTick',[-pi/2,pi/2],'xticklabel',{'-f_{max}' 'f_{max}'});
set(gca,'fontsize',16);
box off;
grid off;
fig_pos=get(gca,'Position');
xp1=fig_pos(1);
xp2=fig_pos(1)+fig_pos(3)+0.02;
yp1=fig_pos(2);
yp2=fig_pos(2)+fig_pos(4)+0.03;
a1=annotation('arrow', [xp1 xp2],[yp1 yp1]);
a2=annotation('arrow', [xp1 xp1],[yp1 yp2]);
I tried to use PlotAxesAtOrigin and axescenter of the FileExchange but due to the annotations this doesn't work properly.
Does anyone know a way to make this work?
Thanks for your help, Klaus!
Moving the y-axis arrow annotation is straight forward. Simply replace your last line with
a2=annotation('arrow', fig_pos(1)+fig_pos(3)/2*[1 1],[yp1 yp2]);
Moving the X(f) is slightly more problematic as you can no longer use the Ytick labels.
Replace your 5th line with
set(gca, 'YTick','');
and add the following line
text(0.1,1.2,'{\color[rgb]{0,0,1}X(f)}','FontSize',16);
at the end of the code.
There's no way to remove the black vertical line that is still on the left side of the axes, so you need to mask it with another annotation. Something like
annotation('line',fig_pos(1)*[1 1],[fig_pos(2) fig_pos(2)+fig_pos(4)],...
'Color',get(gcf,'Color'),'LineWidth',2);
would do.
That gives
A final note is that you create a variable called fig_pos. That is misleading as it contains the axis position (on the figure), not the figure position (which is its position relative to the lower left corner of your monitor). Your variable should really be called axis_pos.
You can use "axis" command, if you know the value of fmax. axis([xmin xmax ymin ymax])
I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')
I use some things from this question get-rid-of-the-white-space-around-matlab-figures-pdf-output to get rid of the white space when saving figure plots and images. This works fine, but my problem is when I use commands like "axis square" or "axis image". Ivt sets TightInset property of corresponding axes to 0 in "y" direction. I mean when I use this:
inset=get(a,'TightInset');
second and fourth numbers in "inset" are always zero, even if I set title and x-label. So in the end I don't see plot titles and x-labels.
Can I fix it somehow? So far I do it manually by adding some suitable number, but it is not convenient.
EDIT: Some more code for example. It displays two histograms.
h=figure;
subplot(121)
bar(bins,histogram1,'hist');
axis square
title('Historgram 1');
xlabel('Intensity');
ylabel('Pixel count');
set(gca,'xlim',[0 1])
subplot(122)
bar(bins,histogram_out,'hist');
axis square
title('Histogram 2');
set(gca,'xlim',[0 1])
xlabel('Intensity');
ylabel('Pixel count');
and if I call
a=get(h,'Children');
for i=1:length(a)
inset=get(a(i),'TightInset');
%...some stuff here
end
those y-related numbers in inset are zeros. If I comment axis square and do this, then inset are correct and it does what I need.
As far as I know when you use 'TightInset' you'll get only the graph or image, axis will be removed. I downloaded a function from mathworks file exchange 'saveTightfigure.m' to solve a similar problem. But I did not needed axes. If you need axes may be you can edit limits set inside the function. I mean you can give a little more space at left and bottom for keeping the axes.
Provided with some code like the following:
x=1:.5:10
plot(x,sin(x))
set(gca,'box','on')
I'm trying to get the left axis to maintain it's tick marks and the right axis to have none.
I know I don't want to do the following:
set(gca,'box','off')
set(gca,'Ytick','[]') %this turns off the left and right axes tick marks. I just want the right off.
I would really,really prefer to not use plotyy. Any help would be appreciated.
Are creating dumby axes the only option here?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/261486
I think that you are stuck using a dummy axes (or a variety of even more unappealing options).
FWIW, the code required is only a few lines; the shortest I can get it is below:
a1 = axes('box','on','xtick',[],'ytick',[]); %Under axis, used as background color and box;
a2 = axes(); %Over axis, used for ticks, labels, and to hold data
propLink = linkprop([a1 a2],'position'); %Keep the positions the same
x=1:.5:10 %Make and plot data
plot(a2, x,sin(x))
set(a2,'box','off','color','none'); %Set top axes props last ("plot" overrides some properties)