I am using plotyy to plot two vectors on different y-axes. I wish to add a third vector to one of the two axes. Can someone please tell me why the following code is not working?
[ax h1 h2] = plotyy(1:10,10*rand(1,10),1:10,rand(1,10));
hold on; plot(ax(2),1:10,rand(1,10));
??? Error using ==> plot
Parent destroyed during line creation
I simply wish to add an additional vector to one of the axes (ax(1),ax(2)) created by plotyy.
Apply hold to the axis of interest.
[ax h1 h2] = plotyy(1:10,10*rand(1,10),1:10,rand(1,10));
hold(ax(2), 'on');
plot(ax(2),1:10,rand(1,10));
plotyy works by creating two axes, one on top of the other. You are carefully adding the new vector to the second axis. The hold property is also a per-axis property, so you just need to make sure that the hold is set on the same axis.
Related
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.
When the graph below is plotted, NSS1 which is simply a constant set equal to one is right on the top border of the graph and thus hard to see.
How can I change the length of the y-axis to say 1.2 so that the NSS1 can be seen more clearly?
lambda=5;
tau=0:30;
tau(1)=0.000001;
NSS1=1*ones(1,31);
NSS2=(1-exp(-tau/lambda))./(tau/lambda);
NSS3=((1-exp(-tau/lambda))./(tau/lambda)-exp(-tau/lambda));
%ylim([0, 1.2])
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
xlabel('t = 0 to 30y', 'FontSize',30)
ylabel('yield','FontSize',30)
The reason why ylim doesn't work if you put it before the plot command is that there is no axes object it can relate to.
So there are two options:
First, you create an axes object and hold it with hold on, so the upcoming plot is plotted on the same axis.
ax = axes; hold on;
ylim([0, 1.2])
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
or second, you plot first, the command automatically generates an axes object and you can modify its y-limits afterwards:
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
ylim([0, 1.2])
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.
I have two signals, one for each axis and taken singularly and plotted in a standard way separately they would look like this:
However they represent different measurements and I need to superimpose them by representing one on the standard X-Y axis (bottom-left) and one on a different set of coordinate axis (X = left, Y = top).
I thought of the following code:
figure(1);
line(1:128,imagesXMatrix(i,:));
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
ax2 = axes('Position',get(ax1,'Position'), ...
'XAxisLocation','left','YAxisLocation','top', ...
'Color', 'none','XColor','k','YColor','k');
line(1:128,imagesYMatrix(i,:),'Parent',ax2);
but am getting the following error:
Error using axes
Bad property value found.
Object Name: axes Property
Name: 'XAxisLocation'.
which I guess means that value left is not ok for the XAxisLocation variable.
What I would really want is simply the superimposition of the two following plots, any idea on how to make it happen?
Thanks a lot!
plotyy will do the trick:
figure(1)
AX = plotyy(x1,y1,x2,y2,'plot');
set(get(AX(1),'Ylabel'),'String','y1')
set(get(AX(2),'Ylabel'),'String','y2')
and additional properties like:
set(AX(1),'ylim',[...],...)
set(AX(2),'ylim',[...],...)
and more information in the documentation.
I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)