MATLAB: a better way to switch between primary and secondary y-axis? - matlab

I'm aware of plotyy but in my opinion it's not as intuitive as for example typing subplot(2,3,1) and from that point one working in that particular subplot's environment...
Suppose I have the following data:
a=rand(20,1);
a_cumul=cumsum(a);
I would like to do a plot of a_cumul on the primary (left hand) y-axis and a bar chart of a on the secondary (right hand) y-axis.
I'm well aware that I can do:
plotyy(1:length(a_cumul),a_cumul,1:length(a),a,'plot','bar')
But this is cumbersome and what if I want to for example plot to the secondary y-axis only and not plot to the primary y-axis? In short, I'm looking for whether a solution like this exists:
figure;
switchToPrimaryYAxis; % What to do here??
plot(a_cumul);
% Do some formatting here if needed...
switchToSecondaryYAxis; % What to do here??
bar(a);
Thanks a lot for your help!

Basically plotyy:
creates two superimposed axes
plots the data specified as the first two params on the first axes
plots the data specified as the last two params on the second axes
set the second second axes color to none making it "transparent" so allowing seeing the graph on the first axes
moves the yaxislocation from the standard position (left) to right
You can create a figure, then two axes make make any plot on the two axes by selecting then with axes(h) where h is the handler of the axes.
Then you can write a your own function performing the axes adjustment.
Script to create figure, axes and call the function to adjust the axes
% Generate example data
t1=0:.1:2*pi;
t2=0:.1:4*pi;
y1=sin(t1);
y2=cos(t2);
% Create a "figure"
figure
% Create two axes
a1=axes
a2=axes
% Set the first axes as current axes
axes(a1)
% Plot something
plot(t1,y1,'k','linewidth',2)
% Set the second axes as current axes
axes(a2)
% Plot something
plot(t2,y2,'b','linewidth',2)
grid
% Adjust the axes:
my_plotyy(a1,a2)
Function to adjust the axes - emulating plotyy behaviour
The function requires, as input, the handles of the two axes
function my_plotyy(a1,a2)
set(a1,'ycolor',[0 0 0])
set(a1,'box','on')
% Adjust the second axes:
% change x and y axis color
% move x and y axis location
% set axes color to none (this make it transparend allowing seeing the
% graph on the first axes
set(a2,'ycolor','b')
set(a2,'xcolor','b')
set(a2,'YAxisLocation','right')
set(a2,'XAxisLocation','top')
set(a2,'color','none')
set(a2,'box','off')
Hope this helps.

Related

How to update a scatter3 plot (in a loop) in Matlab

Quite a simple question but just couldn't find the answer online... I want to visualise a point cloud gathered from a lidar. I can plot the individual frames but wanted to loop them to create a "animation". I know how to do it for normal plots with drawnow but can't get it working with a scatter3. If I simply call scatter3 again like I have done in the commented code then the frame that I am viewing in the scatter plot jumps around with every update (Very uncomfortable). How do i get the scatter3 plot to update to the new points without changing the UI of the scatter ie. Still be able to pan and zoom around the visualised point cloud while it loops through.
EDIT: The file is a rosbag file, I cannot attach it because it is 170MB. The problem doesn't happen when using scatter3 in a loop with a normal array seems to be something with using scatter3 to call a PointCloud2 type file using frame = readMessages(rawBag, i).
EDIT: The problem does not seem to be with the axis limits but rather with the view of the axis within the figure window. When the scatter is initialised it is viewed with the positive x to the right side, positive y out of the screen and positive z upwards, as shown in view 1. Then after a short while it jumps to the second view, where the axis have changed, positive x is now out of the screen, positive y to the right and positive z upwards (both views shown in figures). This makes it not possible to view in a loop as it is constantly switching. So basically how to update the plot without calling scatter3(pointCloudData)?
rawBag = rosbag('jackwalking.bag');
frame = readMessages(rawBag, 1);
scatter3(frame{1});
hold on
for i = 1:length(readMessages(rawBag))
disp(i)
frame = readMessages(rawBag, i);
% UPDATE the 3D Scatter %
% drawnow does not work?
% Currently using:
scatter3(frame{1})
pause(.01)
end
The trick is to not use functions such as scatter or plot in an animation, but instead modify the data in the plot that is already there. These functions always reset axes properties, which is why you see the view reset. When modifying the existing plot, the axes are not affected.
The function scatter3 (as do all plotting functions) returns a handle to the graphics object that renders the plot. In the case of scatter3, this handle has three properties of interest here: XData, YData, and ZData. You can update these properties to change the location of the points:
N = 100;
data = randn(N,3) * 40;
h = scatter3(data(:,1),data(:,2),data(:,3));
for ii = 1:500
data = data + randn(N,3);
set(h,'XData',data(:,1),'YData',data(:,2),'ZData',data(:,3));
drawnow
pause(1/5)
end
The new data can be totally different too, it doesn't even need to contain the same number of points.
But when modifying these three properties, you will see the XLim, YLim and ZLim properties of the axes change. That is, the axes will rescale to accommodate all the data. If you need to prevent this, set the axes' XLimMode, YLimMode and ZLimMode to 'manual':
set(gca,'XLimMode','manual','YLimMode','manual','ZLimMode','manual')
When manually setting the limits, the limit mode is always set to manual.
As far as I understood what you describe as "plots jumpying around", the reason for this are the automatically adjusted x,y,z limits of the scatter3 plot. You can change the XLimMode, YLimMode, ZLimMode behaviour to manual to force the axis to stay fixed. You have to provide initial axes limits, though.
% Mock data, since you haven't provided a data sample
x = randn(200,50);
y = randn(200,50);
z = randn(200,50);
% Plot first frame before loop
HS = scatter3(x(:,1), y(:,1), z(:,1));
hold on
% Provide initial axes limits (adjust to your data)
xlim([-5,5])
ylim([-5,5])
zlim([-5,5])
% Set 'LimModes' to 'manual' to prevent auto resaling of the plot
set(gca, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual')
for i=2:len(x,2)
scatter3(x(:,i), y(:,i), z(:,i))
pause(1)
end
This yields an "animation" of plots, where you can pan and zoom into the data while continuous points are added in the loop

Plot multiple columns with different colors in MATLAB

I have a 372x15 matrix. I'm trying to graph this in such a way that columns 1-14 will be on the x-axis with different colors for each column, whereas the 15th column will be treated as the y-axis. For example, the plot with follow (x1, y), (x2, y) so on so forth, where x1 is all the data points in column 1. This is a simple scatterplot. How can I do this on MATLAB?
A simple way to do that is just use plot(A(:,1:end-1), A(:,end), '.'). Here's an example:
A = [(1:14)-.6*rand(372,14) ((1:372).'+rand(372,1))]; % example A. Uses implicit expansion
plot(A(:,1:end-1), A(:,end), '.') % do the plot
axis tight % optionally make axis limits tight
The above cycles through the 7 predefined colors. If you prefer to customize the colors, set the 'ColorOrder' property of the axes before calling plot, and use hold on to prevent Matlab from resetting it:
clf % clear figure
cmap = autumn(size(A,2)); % example colormap
set(gca, 'ColorOrder', cmap); % set that colormap
hold on % needed so that the colormap is not automatically reset
plot(A(:,1:end-1), A(:,end), '.')
axis tight
You can specify different markers or marker sizes; see plot's documentation.

Vertical lines for Bode plots in Matlab

I have graphed a Bode plot for my transfer function, and I was wondering if there is some way to insert either horizontal or vertical lines to show a specific value for the gain/phase angle or frequency?
I have found with the following code I can draw a horizontal line on the phase angle graph:
x = linspace(10^-1,10^2,100);
for bleh = 1:length(x)
y(bleh) = -30.9638;
end
bode(num, den)
hold on
plot(x,y)
But this does not seem to apply in the gain graph, nor does my limited knowledge (and only way that makes sense to me) of vertical lines. I tried:
y1 = get(gca,'ylim');
w1 = 1.2;
bode(num, den)
hold on
plot(x,y,[w1 w1],y1)
But I only get the one horizontal line as was done from the above code.
Is this a possibility?
(Using R2017a, if that matters.)
I'm not sure I've understood you question, nevertheless, I propose the following.
When there are more one axes in a figure, as it is the case of the bode diagram, if you want to add something in a specific axes (or in all) you have to specify, in the call to plot the handle of the axes.
So, to add lines in the bode diagram, you have first to identify the handles of the two axes: you can do it in, at least two way:
using the findobj function: ax=findobj(gcf,'type','axes')
extract them as the Children of the figure: ax=get(gcf,'children')
Once you have the handles of the axes, you can get their XLim and YLim that you can use to limit the extent of the line you want to add.
In the following example, I've used the above proposed approach to add two lines in each graph.
The horizontal and vertical lines are added in the middle point of the X and Y axes (problably this point does not have a relevant meaning, but it is ... just an example).
% Define a transfer function
H = tf([1 0.1 7.5],[1 0.12 9 0 0]);
% PLot the bode diagram
bode(H)
% Get the handles of the axes
ax=findobj(gcf,'type','axes')
phase_ax=ax(1)
mag_ax=ax(2)
% Get the X axis limits (it is the same for both the plot
ax_xlim=phase_ax.XLim
% Get the Y axis limits
phase_ylim=phase_ax.YLim
mag_ylim=mag_ax.YLim
%
% Define some points to be used in the plot
% middle point of the X and Y axes of the two plots
%
mid_x=(ax_xlim(1)+ax_xlim(2))/2
mid_phase_y=(phase_ylim(1)+phase_ylim(2))/2
mid_mag_y=(mag_ylim(1)+mag_ylim(2))/2
% Set hold to on to add the line
hold(phase_ax,'on')
% Add a vertical line in the Phase plot
plot(phase_ax,[mid_x mid_x],[phase_ylim(1) phase_ylim(2)])
% Add an horizontal line in the Phase plot
plot(phase_ax,[ax_xlim(1), ax_xlim(2)],[mid_phase_y mid_phase_y])
% Set hold to on to add the line
hold(mag_ax,'on')
% Add a vertical line in the Magnitide plot
plot(mag_ax,[mid_x mid_x],[mag_ylim(1) mag_ylim(2)])
% Add an Horizontal line in the Magnitide plot
plot(mag_ax,[ax_xlim(1), ax_xlim(2)],[mid_mag_y mid_mag_y])
Hope this helps,
Qapla'

How to break the axis in matlab

How could one break the x axis in the same figure (not the subplot function)?
You can see an example in line graph in panel b in the following picture, one single row data have been split into three parts as well as the x axis.
the question is how this plot can be achieved in matlab.
(like the panel b in this figure)
Ill leave you an exmaple here using surf.
The trick is inserting nans wherever you want the lines to appear "empty"
z=peaks(100);
% If you want to delete a certain amount of rows/cols
z2=z;
z2(:,10:15)=NaN;
z2(:,50:55)=NaN;
z2(:,75:80)=NaN;
% If you want to separate you data without deleting anything
z3=z;
z3=[z3(:,1:15) nan(size(z3,1),5) z3(:,16:75) nan(size(z3,1),5) z3(:,75:100) ];
% This last bit is only for plotting, so you can try it in your computer
subplot(131)
title('Original')
surf(z,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(132)
title('Deleted columns')
surf(z2,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(133)
title('Separatedd data')
surf(z3,'edgecolor','interp')
axis off
view(2)
axis equal
There are a couple of utilities on the File Exchange that allow you to do that:
Break X Axis
BreakXAxis

MATLAB - How to zoom subplots together?

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’)