yyaxis 'right' - can I have tick marks on both sides?

50 次查看(过去 30 天)
I'm creating a figure to illustrate data availability/quality. It's one plot with no subplots. See attached.
I add a small plot at the bottom of the main plot using yyaxis 'right' and fiddling with the y limits. I can't work out how to put the tick marks and labels on both sides for just this bottom plot.
Can this be done?
Here is the relevant part of my script - it is not possible to share the whole script and the data here.
figure;
plot( XP1, YP1, 'b|', 'MarkerSize', 10 );
plot( XP2, YP2, 'r|', 'MarkerSize', 10 );
stations = [ "MSCP (0.7km)"; "MSUH (1.0km)"; "MSS1 (1.3km)"; "MBFR (2.0km)"; "MBLG (2.1km)"; "MBLY (2.1km)"; "MBRY (2.4km)"; ...
"MBBY (3.3km)"; "MBHA (3.5km)"; "MSMX (3.7km)"; "MBGH (3.8km)"; "MBWH (3.9km)"; "MBWW (5.0km)" ];
nsta = length( stations );
ylim( [0 nsta+2] );
yticks( 1:nsta );
yticklabels( stations );
h=gca;
h.YAxis.TickLength = [0 0];
h.XAxis.TickLength = [0 0];
set(h, 'Ydir', 'reverse');
box on;
yyaxis right;
bar( ngoodSta, 1.0, 'k' );
ylim( [0 60] );
yticks( [0 2 4] );
ax = gca;
ax.YAxis(2).Color = 'k';
  2 个评论
Rohit
Rohit 2022-10-6
Hi,
My initial investigation shows that it is not currently possible to add another data scale on the left y-axis since it is already being used for plotting “YP1”, “YP2” data. Furthermore, it will be misleading since the resulting left y-axis will be representing categorical as well as numerical data. Instead, it is recommended to use the “subplot” function.
Consider the sample example below:
hfig=figure;
x1=[1,2,3,4];
y1=[1,2,2,1];
x2=[2,4,6,8];
y2=[2,3,1,1];
vals=[0,2,4];
subplot(4,1,1:3);
plot( x1, y1, 'b|', 'MarkerSize', 10 );
hold on;
plot( x2, y2, 'r|', 'MarkerSize', 10 );
stations = ["stat1", "stat2","stat3"];
nsta = length( stations );
ylim( [0 nsta+2] );
yticks(1:nsta );
yticklabels( stations );
h=gca;
h.YAxis.TickLength = [0 0];
h.XAxis.TickLength = [0 0];
set(h, 'Ydir', 'reverse');
box on;
subplot(4,1,4);
yyaxis right;
bar( [1,1,2,3,4,2], 1.0, 'k' );
ylim([0,6])
yticks( vals );
ax = gca;
ax.YAxis(2).Color = 'k';
yyaxis left;
ylim([0,6])
yticks( vals );
ax = gca;
ax.YAxis(2).Color = 'k';
Here, dummy data(“x1”,”x2”,”y1”,”y2”,”vals”) has been used for creating a similar figure.
You can refer to the following documentation link for more information on the “subplot” function.:
Adam Danz
Adam Danz 2022-10-6
Also consider turning on the "box" and using grid lines instead of using double labels.
plot(rand(5))
box on
grid on

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by