2012-12-11 41 views
5

Tôi có ba tập dữ liệu và cũng là vectơ lỗi của chúng. Tôi muốn vẽ chúng trên cùng một hình, trong đó các tập dữ liệu nằm trên cùng trục y (ở bên trái) và lỗi nằm trên cùng một hình nhưng với một trục khác (ở bên phải).Vẽ nhiều đồ thị với một trục y và một biểu đồ khác với trục y khác trên cùng một hình trong Matlab

Chức năng plotyy cho phép làm điều đó cho 1 ô trên mỗi trục, làm cách nào để làm điều đó cho một số ô?

Trả lời

5

Cũng có thể sử dụng plotyy() với nhiều hàng giá trị y.

Sau một ví dụ từ sự giúp đỡ matlab:

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',':') 

sản xuất hình sau

Using plotyy with multiple y-values.

+0

+1: tốt nhất chưa –

4

Tôi nghĩ bạn nên tạo mới axes bằng tay:

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: enter image description here

1

Dưới đây là một cái gì đó bạn có thể thử:

% 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', []); 

enter image description here

Tôi đã sử dụng line thay vì plot, vì plot cung cấp cho tôi một số vấn đề về với các dấu kiểm trục y. Thêm thông tin here.

2

Dưới đây là các mã sử dụng Waterloo theo đề nghị của @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'); 

mà sản xuất:

enter image description here

Đối với ví dụ khác thấy here

0

Đây là cách đơn giản nhất để vẽ hai trục y cho phép bạn sử dụng ghi nhãn vọt cùng setters như trong âm mưu bình thường: ylim, ylabel vv

Từ 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'); 
Các vấn đề liên quan