필터 지우기
필터 지우기

Graph a scatter plot on top of a bar graph with different x- and y-axes

조회 수: 10 (최근 30일)
Carson
Carson 2011년 7월 21일
댓글: Jose Vilela 2015년 10월 21일
I need to plot a scatter plot on top of a bar graph. Each of the two plots has both a different x-axis and y-axis. I tried using the instructions for using "Multiple X- and Y-Axes" on Matlab's site to no avail. Here's the code I used:
hl1 = bar(barData', 'stacked');
xlim([0 900]);
ylim([0 1]);
ylabel('Percent of watershed land area');
ax1 = gca;
set(ax1, 'XColor', 'r', 'YColor', 'r');
ax2 = axes('Position', get(ax1, 'Position'), 'XAxisLocation', 'top', ...
'YAxisLocation', 'right', 'Color', 'none', 'XColor', 'k', 'YColor', 'k');
hl2 = scatter(POLN_PLOT, COST_PLOT, 'filled', 'Parent', ax2);
When I run the code, the first graph (bar graph) gets overwritten by the second graph (scatter plot). Both the red and black axes appear on top of one another. What I want is for the black axis to appear at the right and on the top of the plot.
I'm not experienced with using handles or properties, so I was hoping someone might be able to show me the best way to go about this. Thanks in advance!

답변 (1개)

Carson
Carson 2011년 7월 21일
Nevermind - I figured it out myself. You just have to add a few hold() statements:
bar(barData', 'stacked');
xlim([0 900]);
ylim([0 1]);
ylabel('Percent of watershed land area');
ax1 = gca;
set(ax1, 'XColor', 'r', 'YColor', 'r');
hold(ax1, 'all'); % <--------------------------------
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top',...
'YAxisLocation','right','Color','none','XColor','k','YColor','k');
hold(ax2, 'all'); % <--------------------------------
scatter(POLN_PLOT, COST_PLOT, 'filled', 'Parent', ax2);
Hope this helps someone else out and saves them some time!
  댓글 수: 4
Summer
Summer 2014년 9월 4일
YOU ARE MY HERO. I've been struggling with this for hours. Thanks!
Jose Vilela
Jose Vilela 2015년 10월 21일
Thanks a lot, it was so helpful

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

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by