%----------------------------------------------%
%   MatLab graphics    examples                %
%----------------------------------------------%

% Example : using plottools
x = -1:.1:1; 
y = x.^3; 
plottools

% Example : command 

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,'b-')

% Example
x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,'b-',x,y2,'r-.',x,y3,'k-')

% Example: plot3
x=0:0.05:20; y=x.*sin(x).*cos(x); z=x.*cos(x).*cos(x);
plot3(x,y,z,'r.'),grid,box,axis equal

% Example: Adding Plots to an Existing Graph
[x,y,z] = peaks;
pcolor(x,y,z)
shading interp
hold on
contour(x,y,z,20,'k')
hold off

% Example
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X)
subplot(2,2,2); mesh(Y)
subplot(2,2,3); mesh(Z)
subplot(2,2,4); mesh(X,Y,Z)

% Example
axis square
axis equal

% Example: Axis Labels and Titles

t = -pi:pi/100:pi;
y = sin(t);
plot(t,y)
axis([-pi pi -1 1])
xlabel('-\pi \leq {\it t} \leq \pi')
ylabel('sin(t)')
title('Graph of the sine function')
text(1,-1/3,'{\it Note the odd symmetry.}')

% Example
x = -pi:pi/20:pi;
plot(x,cos(x),'-ro',x,sin(x),'-.b')
legend('cos','sin',2);

% Example: Mesh and Surface Plots
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z,'EdgeColor','black')

% Example
surf(X,Y,Z)
colormap hsv
colorbar

surf(X,Y,Z)
colormap hsv
alpha(0.2)

% Example: Bar plot
x = -2.9:0.2:2.9;
bar(x,exp(-x.*x),'r')

Y = round(rand(5,3)*10);
subplot(2,2,1)
bar(Y,'group')
title 'Group'
subplot(2,2,2)
bar(Y,'stack')
title 'Stack'
subplot(2,2,3)
barh(Y,'stack')
title 'Stack'
subplot(2,2,4)
bar(Y,1.5)
title 'Width = 1.5'

% Example: pie charts

X = [19.3 22.1 51.6;
     34.2 70.3 82.4;
     61.4 82.9 90.8;
     50.5 54.9 59.1;
     29.4 36.3 47.0];
     
x = sum(X);  % 求出每列的和
pie(x)
% 使用函数pie的explode参数, 以突出销售量最大的那部分
explode = zeros(size(x));
[c,offset] = max(x);
explode(offset) = 1;
pie(x,explode); colormap summer

% Example: Hist
 
Y = randn(10000,1);
hist(Y)

Y = randn(10000,3);
hist(Y)

% Example: Stem Stairs

alpha = .02; beta = .5; t = 0:4:200;
y = exp(-alpha*t).*cos(beta*t);
plot(t,y)

stem(t,y) % alternative
xlabel('Time in \mu secs')
ylabel('Magnitude')

t = 0:10;
y = exp(-alpha*t).*cos(beta*t);
stairs(t,y)

hold on
plot(t,y,'--*')
hold off
label = 'Stairstep plot of e^{-(\alpha*t)} cos\beta*t';
text(3,1,label,'FontSize',14)
xlabel('t = 0:10','FontSize',14)
axis([0 10 -1.2 1.2])

% Example: Contour plots

[X,Y,Z] = peaks;
[C,h]=contour(X,Y,Z,10)
clabel(C,h)
title({'Contour Labeled Using','clabel(C,h)'})



%---------------------------------
% fplot
fplot('[exp(x),3*sin(x),x^2]',pi*[-1,1,-1,1])

%  Implicit function

% Example: ezplot
ezplot('x^2-y^4')
ezplot('x^2-y^4',[0,10,0,10])

% Example: ezsurf

ezsurf('exp(-s)*cos(t)','exp(-s)*sin(t)','t',[0,8,0,5*pi])

%--------------------------
%  graphic hundle

% Example: set
x = 1:10;
y = x.^3;
h = plot(x,y);
set(h,'Color','red','LineWidth',3)

% Example: get
get(h)
get(h,'Color')
get(0,'format')
get(0,'ScreenSize')

% Example: findobj

x=linspace(0,2*pi);y=sin(x);plot(x,y);
text(pi/4,sin(pi/4),'\leftarrowsin(t) = .707'); 
text(pi,sin(pi),' \leftarrowsin(t)=0');
text(1.8+pi/4,sin(pi+pi/4),'sin(t)=-.707\rightarrow');

text_handle = findobj('String','\leftarrowsin(t) = .707'); 
set(text_handle,'Position',[3*pi/4,sin(3*pi/4),0]);