Reputation: 8931
I have three data sets and also a vector of their error. I want to plot them on the same figure, where the data sets are on the same y-axis (on the left) and the error is on the same figure but with a different axis (on the right).
The function plotyy allows to do it for 1 plot on each axis, how do I do it for several plots?
Upvotes: 6
Views: 3716
Reputation: 3036
This is the simplest way to plot two y axes that allow you to use the same labeling and bounds setters as in normal plotting: ylim
, ylabel
, etc.
From the docs.
yyaxis left;
plot(x, y_left);
ylim([0, 100]); % sets the limits for the left y axis
ylabel('left axis');
yyaxis right;
plot(x, y_right);
ylim([0, 100]); % sets the limits for the right y axis
ylabel('right axis');
Upvotes: 0
Reputation: 71
Here's the code using Waterloo as suggested by @natan
x = 0:0.01:20;
y1 = [200*exp(-0.05*x).*sin(x);300*exp(-0.04*x).*sin(x)];
y2 = [0.8*exp(-0.5*x).*sin(10*x);0.6*exp(-0.4*x).*sin(5*x)];
f=GXFigure();
ax=subplot(f,1,1,1);
p1=line(ax, x, y1(1,:), 'LineColor', 'SEAGREEN');
p2=line(p1, [], y1(2,:), 'LineColor', 'TOMATO');
ax.getObject().getView().setXLabel(sprintf('Time Slow Decay %cs', char(181)));
layer1=kcl.waterloo.graphics.GJGraph.createInstance();
ax.getObject().getView().add(layer1);
p3=line(wwrap(layer1), x, y2(1,:), 'LineColor', 'CORNFLOWERBLUE');
p4=line(p3, x, y2(2,:), 'LineColor', 'CRIMSON');
layer1.setXLabel(sprintf('Time Fast Decay %cs', char(181)));
ax.getObject().setTitleText('Multiple Decay Rates');
which produces:
For other examples see here
Upvotes: 2
Reputation: 3137
It is also possible to use plotyy()
with multiple rows of y-values.
Following an example from the matlab help:
x = 0:0.01:20;
y1 = [200*exp(-0.05*x).*sin(x);
300*exp(-0.04*x).*sin(x)];
y2 = [0.8*exp(-0.5*x).*sin(10*x);
0.6*exp(-0.4*x).*sin(5*x)];
[AX,H1,H2] = plotyy(x,y1,x,y2);
set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')
xlabel('Time (\musec)')
title('Multiple Decay Rates')
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')
producing the following figure
Upvotes: 5
Reputation: 38032
I think you should create the new axes
manually:
figure(1); clf, hold on
x1 = 0:0.1:5*pi;
y1 = sin(x1)./x1;
x2 = 0:0.1:5*pi;
y2 = x2.^(0.2);
x3 = 0:0.1:5*pi;
y3 = cos(x3);
plot(x1,y1, 'b', 'linewidth', 2)
plot(x2,y2, 'g', 'linewidth', 2)
plot(x3,y3, 'k', 'linewidth', 2)
ax1 = gca;
ax2 = axes('Position', get(ax1,'Position'),...
'YAxisLocation','right',...
'Color' , 'none',...
'YColor', 'r');
linkaxes([ax1 ax2], 'x')
x4 = x3;
y4 = 0.025*randn(size(y3));
line(x4, y4, 'color', 'r', 'parent', ax2)
Output:
Upvotes: 4
Reputation: 9642
Here's something you can try:
% Example data
x = [1 2 3];
yd1 = [1 1 1];
yd2 = [2 2 2];
yd3 = [3 3 3];
ye1 = [0.1 0.2 0.3];
ye2 = [0.2 0.3 0.4];
ye3 = [0.3 0.4 0.5];
% Create two axes
ax1 = axes();
ax2 = axes();
% Plot your data
line(x, yd1, 'Parent', ax1, 'Color', 'b');
line(x, yd2, 'Parent', ax1, 'Color', 'b');
line(x, yd3, 'Parent', ax1, 'Color', 'b');
line(x, ye1, 'Parent', ax2, 'Color', 'r');
line(x, ye2, 'Parent', ax2, 'Color', 'r');
line(x, ye3, 'Parent', ax2, 'Color', 'r');
% Modify axes properties
set(ax1, 'ylim', [-10 4]);
set(ax2, 'Color', 'none', 'YAxisLocation', 'right', 'XTick', []);
I have used line
instead of plot
, because plot
gives me some
issues with the y-axis ticks. More information here.
Upvotes: 1