When I plot it comes with both axes in the box. How can I plot by removing right axis (or vertical line) of Box? can you please help me?
x=(1:1:10);
y= (1:2:20);
figure (1);
plot (x,y,'o-');

댓글 수: 4

Scott MacKenzie
Scott MacKenzie 2021년 6월 14일
Your description is a bit vague. Is this what you are looking for...
Babu Sankhi
Babu Sankhi 2021년 6월 14일
편집: Babu Sankhi 2021년 6월 14일
Yes you are close I just want to remove right axis but not the top one. BTW how did you do it?
Thank you
Scott MacKenzie
Scott MacKenzie 2021년 6월 14일
편집: Scott MacKenzie 2021년 6월 14일
x = (1:1:10);
y = (1:2:20);
figure (1);
plot (x,y,'o-');
ax = gca;
ax.Box = 'off';
I don't think it's possible to remove just the right box border.
One thing you can do is move the border away from the plot by setting XLim. If your goal is just to create space for annotations to add to the plot, this might work:
x = (1:1:10);
y = (1:2:20);
figure (1);
plot (x,y,'o-');
ax = gca;
ax.XLim = [1 15];
Adam Danz
Adam Danz 2021년 6월 14일
Babu Sankhi's answer moved here as a comment.
yes you are right I want to add another plot by removing the right boarder. Can please give me the more idea about it how can get the plot like this as attched below? thank you

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2021년 6월 14일
편집: Adam Danz 2021년 6월 16일

0 개 추천

Turn off right-y-axis
This solution is similar to another answer provided a few days ago. It uses yyaxis to add a second, independent y-axis on the right and then turns the color off for that axis.
x=(1:1:10);
y= (1:2:20);
figure (1);
plot (x,y,'o-');
yyaxis right
ax = gca();
ax.YAxis(2).Color = 'none';
yyaxis left % return focus to 'main' axis.
Join 2 axes, control the center border line
Update following an example added by OP
This version joins 2 axes, turns off the y-axes in the center of the two axes, formats the left-y-axis of the right-axes as a dashed line, and then links the two sets of y-axes so they always have the same y-limit.
The juxtaposition of the axes uses TiledLayout with zero-tile-spacing which requires Matlab r2021a (see Community Highlight). For versions of Matlab prior to R2021a, create the two axes using the axes command and set their position properties (see version-2 below).
x = 0:0.1:10;
y = gaussmf(x,[2 5]);
figure()
tiledlayout(1,2,'TileSpacing','none'); % Requires Matlab r2021a or later
ax1 = nexttile();
plot(ax1, x,y)
yyaxis(ax1,'right')
ax1.YAxis(2).Color = 'none';
yyaxis(ax1,'left') % return focus to 'main' axis.
xlabel('Left Axis')
ax2 = nexttile();
plot(ax2, x,y)
yyaxis(ax2,'right')
ax2.YAxis(2).Color = ax1.YAxis(1).Color;
ax2.YAxis(2).TickLabels = []; % Turn off right tick labels
yyaxis(ax2,'left') % return focus to 'main' axis.
ax2.YAxis(1).TickValues = [];
ax2.YAxis(1).Axle.LineStyle = 'dashed';
linkaxes([ax1,ax2],'y') % link both sets of y-axis limits
xlabel('Right Axis')
Version-2 using custom axes instead of TiledLayout
x = 0:0.1:10;
y = gaussmf(x,[2 5]);
figure()
margins = [.13 .11 .25]; % [left&right, bottom, top], normalized units
ax1 = axes('Units','Normalize','Position',[margins(1:2),.5-margins(1),1-margins(3)]);
plot(ax1, x,y)
yyaxis(ax1,'right')
ax1.YAxis(2).Color = 'none';
yyaxis(ax1,'left') % return focus to 'main' axis.
xlabel('Left Axis')
ax2 = axes('Units','Normalize','Position',[.5,margins(2),.5-margins(1),1-margins(3)]);
plot(ax2, x,y)
yyaxis(ax2,'right')
ax2.YAxis(2).Color = ax1.YAxis(1).Color;
ax2.YAxis(2).TickLabels = []; % Turn off right tick labels
yyaxis(ax2,'left') % return focus to 'main' axis.
ax2.YAxis(1).TickValues = [];
ax2.YAxis(1).Axle.LineStyle = 'dashed';
linkaxes([ax1,ax2],'y') % link both sets of y-axis limits
xlabel('Right Axis')
Additional recommendations
  • Set the x-ticks to avoid the overlap at the x-limits.
  • Turn the grid on for both pairs of axes (yyaxis left)

댓글 수: 8

Babu Sankhi
Babu Sankhi 2021년 6월 14일
편집: Babu Sankhi 2021년 6월 14일
But if I want to make left axis off, it does not work ! can you please help me more?
Adam Danz
Adam Danz 2021년 6월 14일
편집: Adam Danz 2021년 6월 15일
Your question was "How can I plot by removing right axis ".
You could apply the same logic in my answer to turn off the left axis too.
I've updated my answer to work that out for you.
Babu Sankhi
Babu Sankhi 2021년 6월 14일
편집: Babu Sankhi 2021년 6월 14일
Thanks I use matlab R2019a so your idea about the tielspacing does not work for me.
How ever I want to make left axis off, it does not work ( it gave the same i.e right axis off) ! can you please help me more? I tried :
Thank you very much for your time
x=(1:1:10);
y= (1:2:20);
figure (1);
plot (x,y,'o-');
yyaxis left
ax = gca();
ax.YAxis(2).Color = 'none';
yyaxis right % return focus to 'main' axis.
Adam Danz
Adam Danz 2021년 6월 14일
편집: Adam Danz 2021년 6월 15일
> your idea about the tielspacing does not work for me
From my answer, "For versions of Matlab prior to R2021a, create the two axes using the axes command and set their position properties."
For example, axes('Units','Normalize','Position',[----]). You just need to set the positions of the two axes so they are next to each other. The positions are 1x4 vectors defining
  1. Distance from the left of the figure (normalized coordinates)
  2. Distance from the bottom of the figure
  3. Width
  4. Height
> I want to make left axis off, it does not work
Actually your question was "How can I plot by removing right axis? " but my answer shows how to turn off both the right and left axes. You haven't followed the example correctly. Try to copy the example as close as possible including the use of axis handles and the order that you're accessing the left and right y-axes. I don't know how else to show you without writing the exact example I already have in my answer. You applied it incorrectly so go line-by-line comparing your version with mine and try to think about what each line is doing.
I am sorry its really difficult to understand as I am a beginner. here is my example plot where I want to eliminate right (or left axis) axis. can you help me more .
Thank you.
x1=(1:1:10);
y1= (1:2:20);
x2=[1,2,3,4,5];
y2=[0.1,3,6,7,2];
h1=plot (x1,y1,'r-'); hold on;
h2=plot (x2,y2,'k-'); hold off
set(h1 ,'LineWidth',2.5);
set(h2 ,'LineWidth',2.5);
set(gca,'yticklabels',[]);
set(gca,'FontSize',18)
legend('run2right','run2rghtfit')
set(gcf,'Position',[100 100 600 500])
ylabel('Intensity (a.u.)');
xlabel('Frequency (Hz)')
Adam Danz
Adam Danz 2021년 6월 15일
편집: Adam Danz 2021년 6월 15일
I'm happy to help.
You need to use the 2nd block of code in my answer (under "Join 2 axes, control the center border line").
All you need to do is
  1. Copy-paste the entire block of code except for the first two lines (you don't need x=..., y=...).
  2. Replace this line plot(ax1, x,y) with plot(ax1,x1,y1)
  3. Replace this line plot(ax2, x,y) with plot(ax2,x2,y2)
That's it. It's really that simple.
Thank yo But it didnt help as I am using 2019a version of martlab. For example I tries this one. It gave me blank!!! Can you make left axis off for the following example. I am so sorry
x1=(1:1:10);
y1= (1:2:20);
x2=[1,2,3,4,5];
y2=[0.1,3,6,7,2];
figure (1);
ax1 = axes('Position',[100 100 600 500]);
h1=plot (ax1,x1,y1,'r-'); hold on;
h2=plot (ax1,x2,y2,'k-'); hold off
set(h1 ,'LineWidth',2.5);
set(h2 ,'LineWidth',2.5);
yyaxis(ax1,'right')
ax1.YAxis(2).Color = 'none';
yyaxis(ax1,'left') % return focus to 'main' axis.
xlabel('Left Axis')
set(gca,'FontSize',18)
set(gcf,'Position',[100 100 600 500])
Adam Danz
Adam Danz 2021년 6월 16일
편집: Adam Danz 2021년 6월 20일
The reason you have a blank figure is because your axes size is 600x500 but axis units are normalized by default so the largest size is 1x1.
I'll update my answer with a demo using custom axes.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics에 대해 자세히 알아보기

질문:

2021년 6월 14일

편집:

2021년 6월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by