How can i put the same labels also on both left and right Y axes (tried several solutions but can't make them work as I wish)?
boxplot(rand(100,10))
Thanks
Oleg

 채택된 답변

Jiro Doke
Jiro Doke 2011년 3월 18일

2 개 추천

I can only think of a workaround...
EDIT: Per Oleg's comment, I've added a "ResizeFcn" for the figure to adjust the axes properties when the figure is resized.
EDIT 2: Added zoom post action so that the tick labels update upon zoom as well.
function boxplot_test
boxplot(rand(100,10));
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'), ...
'Color', 'none', 'YAxisLocation', 'right', ...
'XTick', [], 'YTick', get(ax1, 'YTick'), ...
'YLim', ylim(ax1), 'HandleVisibility', 'off');
uistack(ax1);
set(gcf, 'ResizeFcn', @resize);
h = zoom(gcf); % <-- EDIT 2
set(h, 'ActionPostCallback', @resize); % <-- EDIT 2
function resize(varargin)
set(ax2, 'Position', get(ax1, 'Position'), ...
'YTick', get(ax1, 'YTick'), ...
'YLim', ylim(ax1));
end
end

댓글 수: 5

Oleg Komarov
Oleg Komarov 2011년 3월 18일
When you enlarge the figure the right axes doesn't correspond to the left one anymore.
Jiro Doke
Jiro Doke 2011년 3월 19일
I modified my answer to include ResizeFcn for the figure.
Oleg Komarov
Oleg Komarov 2011년 3월 19일
I'll try this version asap.
Oleg Komarov
Oleg Komarov 2011년 3월 19일
+1. Thanks Jiro.
Paul
Paul 2013년 6월 6일
편집: Paul 2013년 6월 6일
AWSM work-around! :) Thanks so much.
However, I have one suggestion...
If you replace the line
uistack(ax1);
with
axes(ax1);
The user is able to still interact with the initial plot (i.e. use data cursor, etc)

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

추가 답변 (3개)

Matt Fig
Matt Fig 2011년 3월 19일

0 개 추천

Hey Oleg, I don't have the Stats toolbox so I can't try it out. But I have an idea. Make one figure with plotyy, then make another with the boxplot you want. Then either COPYOBJ the boxplot over to the plotyy figure or set the parent property of the boxplot to one of the plotyy axes. It might be worth a try...

댓글 수: 4

Oleg Komarov
Oleg Komarov 2011년 3월 19일
The idea was good...till I realized that the xlabels are text objects (created in order to eliminate the xticks marks w/o touching the yticks) which again don't reposition upon resize.
I'm starting to hate matlab graphics:
why the ticklength affects both X and Y axes? why???
Jiro Doke
Jiro Doke 2011년 3월 19일
Nice. I think that may work. You may also need to "synch" the left and right axes because plotyy sets the ticks for left and right based on the data that is plotted. If you just copy the data on one of the axes, the ticks may not line up. I suppose you can copy the data from boxplot on both the axes.
Oleg Komarov
Oleg Komarov 2011년 3월 19일
Yes, but the problem is that I don't want the tick marks on the X axes, but I can't set Xtick, [] and keep the XtickLabels or TickLenght [0,0] and the tick marks on the Y axes!
Jiro Doke
Jiro Doke 2011년 3월 19일
Yeah, I noticed that. I would go with my solution above, because that uses boxplot as is. Even though the x labels are text objects, they do the "right thing" even when you zoom in and out.

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

Elena
Elena 2011년 6월 5일

0 개 추천

also a bit of a workaround: make a plotyy where one the first dataset is empty or all zeros, then use 'hold on' to plot the boxplot over top.
Hussein
Hussein 2023년 8월 20일

0 개 추천

Try this code please
% Create some sample data x = 1:10; y1 = rand(1, 10); y2 = rand(1, 10) * 100;
% Plot the first dataset figure; plot(x, y1, 'b-'); xlabel('X'); ylabel('Y1', 'Color', 'b');
% Add a second Y-axis yyaxis right;
% Plot the second dataset plot(x, y2, 'r-'); ylabel('Y2', 'Color', 'r');
% Set the color of the tick labels for the right Y-axis ax = gca; ax.YAxis(2).Color = 'r';
% Add a legend legend('Y1', 'Y2');

카테고리

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

질문:

2011년 3월 18일

답변:

2023년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by