plot and left axis off
68 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Scott MacKenzie
el 14 de Jun. de 2021
Editada: Scott MacKenzie
el 14 de Jun. de 2021
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
el 14 de Jun. de 2021
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
Respuestas (1)
Adam Danz
el 14 de Jun. de 2021
Editada: Adam Danz
el 16 de Jun. de 2021
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
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 comentarios
Ver también
Categorías
Más información sobre Graphics Object Properties en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!