Reputation: 199
I want to use plot and semilog in the same graph so as to compare results. I get only the one waveform though using hold on function. What can I do ? I cant use yyaxis left cause my matlab is lower than 2016. Any help?
Here is my code:
figure
semilogy(VG, ID3)
hold on
plot(VG, ID3)
hold off
Upvotes: 2
Views: 1428
Reputation: 5190
A possible solution to have both the linear and the semilog chart in the same graph could be to create two overlapping graphs:
The main steps are:
figure
axes
in the figureplot
axes
in the figure and set its size equal to the one of the first axes (now the secon axes is the one "active")semilogy
Since we have changed the location of the X and Y axis of the second axes, we need to adjust the dimenisons of the figure and of the axes so thay fit toghter
Now you can fix the problem of the different grid of the two axes:
to do that, you can add a menu item to the menu bar (with the uimenu
function) to toggle the grid
In the following, a possible implementation of the suggested approach.
Since you have not specified if you use R2014a or R014b, in the code below you can find both the way to set the properties of the figure and axes:
get
/ set
dot notation
available from R2014b(the last one is "commented")
% Define input data
x=linspace(0,30,30)
y=rand(30, 1);
% Cretate a Figure
f=figure('units','normalized')
% Create the first axes in the figure
ax1=axes
% Plot the data with "PLOT"
ph=plot(x,y,'r')
% Set the axis labeks
xlabel('X data')
ylabel('Y data, LIN mode')
% Get the position of the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1_pos=ax1.Position
ax1_pos=get(ax1,'position')
% Set the color of the fist axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.XColor='r'
% % % % % % % % % % % % % % % % % % % % % % % % ax1.YColor='r'
set(ax1,'xcolor','r','ycolor','r')
% Add the second axes in the figure
ax2=axes('position',ax1_pos)
% Plot the data with SEMILOGY
slh=semilogy(x,y,'b')
% Set the ylabel
ylabel('Y data, LOG mode')
% Set the title of the chrt
title('Linear and Semilog Plot')
% Move the X axis location to the top of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.XAxisLocation='top'
set(ax2,'XAxisLocation','top')
% Move the XYaxis location to the right of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.YAxisLocation='right'
set(ax2,'YAxisLocation','right')
% Set the color of the second axes to transparent
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.Color='none'
set(ax2,'color','none')
% Set the color of the second axes
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.XColor='b'
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.YColor='b'
set(ax2,'xcolor','b','ycolor','b')
% Add the lgend to the chart
legend([ph slh],'plot','semilogy','location','best')
% Adjust the size of the the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.Position=[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9]
set(ax1,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% et the size of the second axes as per the first one
ax2.Position=ax1.Position
set(ax2,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% Adjust the size of the figure
% % % % % % % % % % % % % % % % % % % % % % % % % % % fp=f.Position
fp=get(f,'position')
% % % % % % % % % % % % % % % % % % % % % % % % % % % f.Position=[0.1,0.1,fp(3)*1.2,fp(4)*1.2]
set(f,'position',[0.1,0.1,fp(3)*1.2,fp(4)*1.2])
% Add the menu to manage the grid
mh=uimenu('Label','Grid manag.')
m1h=uimenu(mh,'Label','Linear Grid','callback', ...
'axes(ax1);grid;axes(ax2);if(strcmp(m1h.Checked,''off'')),m1h.Checked=''on'';else,m1h.Checked=''off'';end')
m2h=uimenu(mh,'Label','Log Grid','callback', ...
'axes(ax2);grid;if(strcmp(m2h.Checked,''off'')),m2h.Checked=''on'';else,m2h.Checked=''off'';end')
Upvotes: 3
Reputation: 3396
I often have to make figures comparing data in linear and log space. Stacking the plots using subplot()
gives a nice visual. Example:
% set up dummy data that is evenly-space in logspace
x = 10.^((linspace(log10(.01), log10(10),20))');
y = rand(20, 1);
% finish setup
figure
subplot(2,1,1)
plot(x, y, 'DisplayName', 'MyData')
title('Plot with Linear Axes')
xlabel('X-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
subplot(2,1,2)
semilogx(x, y, 'DisplayName', 'MyData')
title('Plot with LogX Axis')
xlabel('LogX-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
Upvotes: 1