Catalog
  1. 1. Data Visualization Via MATALB 1 dimensional data
    1. 1.1. plot multi lines in the same figures
    2. 1.2. Plot different lines by using different y axis
    3. 1.3. Plot the interpolation lines with its origin data
Data Visualization Via MATALB (1d line plots)

create time: 2019 03 20

Data Visualization Via MATALB 1 dimensional data

MATLAB is a power software in scientific calculations and its plot ability is also useful. I want to summarize the most frequently used MATLAB code accoring to my usage.

plot multi lines in the same figures

The first code is how we plot multilines in the same plot. I want to add some extra settings in this code for the default settings are not so beautiful. The following is the plot of different functions:

1
numx=200;
2
x=linspace(-1,10,numx);
3
y1=exp(x);
4
y2=sin(x)+exp(-x);
5
y3=x.^3+x.^2+1;
6
y4=sin(x).*exp(-x);
7
%%
8
figure(1)
9
plot(x,y1,'linewidth',2)
10
hold on
11
plot(x,y2,'linewidth',2)
12
hold on
13
plot(x,y3,'linewidth',2)
14
hold on
15
plot(x,y4,'linewidth',2)
16
hold on
17
set(gca,'linewidth',2,'fontsize',15)
18
xlabel('x','fontname','times newroman','fontsize',15)
19
ylabel('y','fontname','times newroman','fontsize',15)
20
%xlim([0,10])
21
ylim([-5,10])
22
legend({'lin1','lin2','lin3','lin4'},'fontname','times newroman','fontsize'...
23
    ,15,'Location','northwest','Orientation','horizontal')

In above figures I redefined the linewidth of the lines and ticks,changed the fontsize of legend,label etc.
Basic 1d plot

Plot different lines by using different y axis

In some cases we need to plot different things in the smae plot but their number and meanings are different and are not suitable compared directly. We may need the double yy plot.

1
x = linspace(0, 10);
2
y1 = sin(3*x);
3
y2 = sin(3*x) .* exp(0.5*x);
4
5
figure(1)
6
subplot(2,1,1)
7
yyaxis left;
8
plot(x,y1,'Linewidth',1);
9
title('yyaxis');
10
xlabel('X-axis','fontname','times newroman','fontsize',15);
11
ylabel('left Y-axis','fontname','times newroman','fontsize',15);
12
13
yyaxis right;
14
plot(x,y2,'Linewidth',1);
15
ylim([-150,150]); 
16
ylabel('right Y-axis','fontname','times newroman','fontsize',15); 
17
%set(gca,'linewidth',1.5,'fontsize',15)
18
% plotyy
19
subplot(2,1,2)
20
x = 0:0.01:20;
21
y1 = 200*exp(-0.05*x).*sin(x);
22
y2 = 0.8*exp(-0.5*x).*sin(10*x);
23
[hAx, hLine1, hLine2] = plotyy(x,y1, x,y2);
24
25
title('plot yy');
26
xlabel('Time (\musec)');
27
28
ylabel(hAx(1), 'Slow Decay'); % left y-axis
29
ylabel(hAx(2), 'Fast Decay'); % right y-axis

plot by using different yaxis

Plot the interpolation lines with its origin data

We sometimes need to draw some discrete data into a line. In order to look good, we need a smooth curve. In this case, we need to use interpolation. We then give an example to show how to plot . There are two different methods: “plotyy” and “yyaxis”

1
Nx=12;
2
power=zeros(Nx,2);
3
power(:,1)=linspace(1,10,Nx);
4
power(:,2)=[3,6,4,2,1,5,8,10,4,3,2,9];
5
xmin=power(1,1);
6
xmax=power(Nx,1);
7
deltax=(xmax-xmin)/(Nx-1)/10;
8
xi=xmin:deltax:xmax;
9
yi=interp1(power(:,1),power(:,2),xi,'spline');
10
11
figure(1)
12
plot(power(:,1),power(:,2),'>',xi,yi,'Linewidth',2);
13
%axis([4.5 22.5 10.2 10.7])
14
xlabel('x','fontname','times newroman','fontsize',15)
15
ylabel('y','fontname','times newroman','fontsize',15)
16
legend({'origin','interp'},'fontname','times newroman','fontsize'...
17
    ,15,'Location','northwest','Orientation','horizontal')
18
grid
19
set(gca,'linewidth',1.5,'fontsize',15)

Plot from discrete date and do Interpolation

Author: Knifelee
Link: https://knifelees3.github.io/2019/04/06/A_En_MATLAB_PlotSummary1_1dLinePlots/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶

Comment