双坐标曲线的画法-yyaxis,plotyy-Create ch

2022-06-05  本文已影响0人  思求彼得赵

2022-06-02


Description

yyaxis left activates the side of the current axes associated with the left y-axis. Subsequent graphics commands target the left side. If the current axes do not include two y-axes, then this command adds a second y-axis. If there are no axes, then this command first creates them.

yyaxis right activates the side of the current axes associated with the right y-axis. Subsequent graphics commands target the right side.

yyaxis([ax] specifies the active side for the axes ax instead of the current axes. If the axes do not include two y-axes, then this command adds a second y-axis. Specify the axes as the first input argument. Use single quotes around 'left' and 'right'.

Plot Data Using Two y-Axes

//openExample('graphics/PlotDataUsingTwoYAxesExample')
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)

z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])

image.png

eg. 2. Add Title and Axis Labels to Each Side

load('accidents.mat','hwydata')
ind = 1:51;
drivers = hwydata(:,5);
yyaxis left
scatter(ind,drivers)
title('Highway Data')
xlabel('States')
ylabel('Licensed Drivers (thousands)')
pop = hwydata(:,7);
yyaxis right
scatter(ind,pop)
ylabel('Vehicle Miles Traveled (millions)')
image.png

Plot Multiple Sets of Data on Each Side

x = linspace(0,10);
yl1 = sin(x);
yl2 = sin(x/2);
yyaxis left
plot(x,yl1)
hold on
plot(x,yl2)
yr1 = x;
yr2 = x.^2;
yyaxis right
plot(x,yr1)
plot(x,yr2)
hold off

image.png

Control Colors for Each Side

colororder({'b','m'})

yyaxis left
y = [1 2; 3 4];
plot(y)

yyaxis right
z = [4 3; 2 1];
plot(z)
legend

Control Individual Plot Colors

yyaxis left
bar(magic(3));
colororder('default')
yyaxis right
scatter([1 2 3],[2 5 2],'filled')
hold on
scatter([1 2 3],[3 4 1],'filled')
scatter([1 2 3],[4 2 4],'filled')
hold off
colororder({'r','b','c'})
legend
image.png

Add Second y-Axis to Specific Axes

x = linspace(1,10);
tiledlayout(2,1)

% Top plot
ax1 = nexttile;
yyaxis(ax1,'left')
plot(ax1,x,sin(x))
yyaxis(ax1,'right')
plot(ax1,x,exp(x))

% Bottom plot
ax2 = nexttile;
plot(ax2,1:10)
上一篇下一篇

猜你喜欢

热点阅读