Plot with multiple axes but only one legend - matlab

The following code produces a plot with two lines reffering to the left y-axis and one line referring to the right y-axis. Both plots have their own legend, but I want only one legend listing all 3 lines. I tried to just put the 'y1','y2' strings into the 2nd legend-command as well, but that didn't work out.
line(x,y1,'b','LineWidth',2)
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2)
legend('y1','y2');
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
linkaxes([ax1,ax2],'x');
line(x,y3,'Parent',ax2,'LineWidth',2,'Color',[255,127,80]/256)
legend('y3')

This is a tricky problem since the legend are somehow connected to the axes. Since you will be creating 2 axes, hence there will be 2 legends. However there is a trick to achieve what you want. Firstly, plot all line on the same axes, then run legend. Then create 2nd axes and then move the third line to the 2nd axes. So your code should look like this:
% line
line(x,y1,'b','LineWidth',2)
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2)
l3=line(x,y3,'LineWidth',2,'Color',[255,127,80]/256)
% legend
legend('y1','y2','y3');
% 2nd axes
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color','none','YColor',[255,127,80]/256);
linkaxes([ax1,ax2],'x');
% move l3 to 2nd axes
set(l3,'Parent',ax2);
If you want to use 'DisplayName', it meant to be used with line
line(x,y1,'Color','b','LineWidth',2,'DisplayName','y1');
line(x,y2,'Color',[0,0.6,0.5],'LineWidth',2,'DisplayName','y2');
legend('show');

Related

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'

Plotting in GUI of Matlab

Until now i had only 1 axis in my GUI to I used to just plot directly using plot command. Plus i need to plot these in a loop.
for i = 1:length(sig)
plot(sig(i).time,sig(i).signal,sig(i).time,updated(i).filter,)
hold on
end
Now i have 2 axes in my GUI, how can I make a certain plot appear in 1st axis and another in my 2nd axis
Now for example i need to plot the below in the 2nd axis
for i = 1:length(sig)
plot(sig(i).time,sig(i).fil,sig(i).time,updated(i).sig,)
hold on
end
Any help will be appriciated
You could specify the axes for hold and plot functions. Considering you have two axes, h1 and h2 inside your figure, you could do the following:
hold(h1, 'on')
hold(h2, 'on')
for i = 1:length(sig)
plot(h1, sig(i).time,sig(i).signal,sig(i).time,updated(i).filter)
plot(h2, sig(i).time,sig(i).fil,sig(i).time,updated(i).sig)
end

MATLAB: How to create double axis for one graph

I am trying to create a plot of failure strength vs material stiffness. However, the stiffness can be given as either [Pascal] or [Shore A] - I would like to use both in a double axis plot.
I've tried using plotyy but it will not allow me to have one line plot, nor does it allow the non-linear relation between Pascal and Shore A. I would like to plot one of them and then manually add the spacing between the others ticks.
Preferably I would like the stiffness on the x-axis, but y-axis can do if it is easier.
Any help is most welcome!
Example picture of what I'm trying to do
This may help you when you need double x-axes and double y-axes plot, but if you need single y-axes and two x-axes you can modify accordingly:
This example shows how to create a graph using the bottom and left sides of the axes for the first plot, and the top and right sides of the axes for the second plot.
Create the data to plot.
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
Use the line function to plot y1 versus x1 using a red line. Set the color for the x-axis and y-axis to red.
Note: Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead, such as set(ax1,'XColor','r').
figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
Create a second axes in the same location as the first axes by setting the position of the second axes equal to the position of the first axes. Specify the location of the x-axis as the top of the graph and the y-axis as the right side of the graph. Set the axes Color to 'none' so that the first axes is visible underneath the second axes.
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
Use the line function to plot y2 versus x2 on the second axes. Set the line color to black so that it matches the color of the corresponding x-axis and y-axis.
line(x2,y2,'Parent',ax2,'Color','k')
The graph contains two lines that correspond to different axes. The red line corresponds to the red axes. The black line corresponds to the black axes.
Source:
http://www.mathworks.com/help/matlab/creating_plots/graph-with-multiple-x-axes-and-y-axes.html

Plotting ticks and custom grid lines at the same time

I am trying to make a figure in Matlab that has grid lines at some custom places, but I also want to write ticks at regular intervals. Currently I produced the following graph, with the grid lines in the right position:
plot(myData);
xlabel('Frequency');
ylabel('Maginute');
set(gca, 'XTick', listOfTheoreticalValues);
set(gca,'XGrid', 'on');
set(gca, 'XTickLabel', '');
The problem I am facing now, however, is that I can't put normal, equally spaced ticks on the x-axis, let alone with marking values, because that would immediately add extra grid lines too. Is there a way to separate the two things from each other?
As Hugh Nolan suggested, manually adding grid lines is one way to solve the problem. The following code will do the trick:
%Grid line locations
x_lines = listOfTheoreticalValues;
y_limits = [lower_y_limit; upper_y_limit]; %Insert desired y-limits here
y_grid = repmat(y_limits, 1, numel(x_lines));
x_grid = [x_lines; x_lines];
plot(x_grid, y_grid, ':', 'color', [1,1,1]/2); %First plot grid lines
hold on
plot(myData); %Then plot data to draw data on top of grid lines
xlabel('Frequency');
ylabel('Maginute');

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.