Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple x axes for same y axis in scatter plot

1,359 views
Skip to first unread message

Thor

unread,
Dec 16, 2011, 8:14:08 AM12/16/11
to
I want to plot two variables with the same y-axis dependency, but I cannot make it work. It is no problem for a line plot, but as soon as I try the plot function or scatter function, it will not let me add an extra x-axis on top of the graph. Instead in somehow overwrites the old plot and adds the axis to the bottom, although I specify it to be on the top. This is what I've tried:

D = linspace(-100,0,50);
S = linspace(34,32,50);
T = 10*exp(D/40);

hl1=scatter(S,D);
hold on;
ax1=gca;
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top');
hl2=scatter(T,D,'Parent',ax2);

I think it should be quite simple but I can't figure out whats wrong. If it was just a line plot, there would be no problem as can also be read here: http://www.mathworks.se/help/techdoc/creating_plots/f1-11215.html.

Hope someone can help.

Steven_Lord

unread,
Dec 16, 2011, 9:19:34 AM12/16/11
to


"Thor " <thor.ma...@gmail.com> wrote in message
news:jcfg70$fpc$1...@newscl01ah.mathworks.com...
> I want to plot two variables with the same y-axis dependency, but I cannot
> make it work. It is no problem for a line plot, but as soon as I try the
> plot function or scatter function, it will not let me add an extra x-axis
> on top of the graph. Instead in somehow overwrites the old plot and adds
> the axis to the bottom, although I specify it to be on the top. This is
> what I've tried:
>
> D = linspace(-100,0,50);
> S = linspace(34,32,50);
> T = 10*exp(D/40);
>
> hl1=scatter(S,D);
> hold on;
> ax1=gca;
> ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top');

This creates an entirely new axes object on top of your existing one rather
than simply creating a new X axis line.

What you want is PLOTYY or perhaps PLOTXX (or similar submissions) from the
MATLAB Central File Exchange.

http://www.mathworks.com/matlabcentral/

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Thor

unread,
Dec 18, 2011, 10:48:08 AM12/18/11
to
Well, that's also what I thought so I tried the plotxx script, but I cannot make it work with points instead of a line. I need the data to be plotted with points and not with a line and thus I've tried changing the

hl1=line(x1,y1,'Color','k');

to

hl1=plot(x1,y1,'.');

or "scatter" instead of "plot", but the plotxx-script, or this way of adding an x-axis, only seems to work with the "line"-command.

Andra

unread,
Jan 3, 2012, 11:43:08 AM1/3/12
to
"Thor " <thor.ma...@gmail.com> wrote in message <jcl1vo$242$1...@newscl01ah.mathworks.com>...
I am having the same trouble--I'm trying to make a scatter plot with two y axes and haven't been able to figure out how to do so. Is this possible to do in Matlab?

Tammy

unread,
Apr 25, 2012, 4:02:12 PM4/25/12
to
Has anyone found a solution to this issue? I too am having the same problem. I'm attempting to use plotyy, which works great for line plots, but I can't seem to get it to work with scatter plots.

Thanks!

dpb

unread,
Apr 25, 2012, 5:03:54 PM4/25/12
to
Works w/ the handle call for the plotting function(s) here (that goes
back to R14, even).

plotyy(x1,y1,x2,y2,@scatter)

doc plotyy % and friends for more details.

--

Michael DI MATTEO

unread,
Jan 29, 2015, 9:32:11 PM1/29/15
to
"dpb" wrote in message <jn9orp$uo5$1...@speranza.aioe.org>...
Thanks, dpb. The following code plots two series of scatter data on one plot with two y tick labels.

%% Plot data with two y tick labels
[AX,H1,H2] = plotyy(x,y1,x,y2,@scatter,@scatter); %[axes handle, line1 h, line2 h]

set(...
get(AX(1),'Ylabel'),...
'String', 'Total Nitrogen (%removal of annual load)',...
'Fontsize',15 ...
)
set(get(AX(2),'Ylabel'),'String','Reuse (%requested supplied)','Fontsize',15)

set(AX(1),'Xlim',[(min(x)-1) (max(x)+1)],...
'Ylim',[(min(y1)-1) (max(y1)+1)],'YTick', (35):10:85)
set(AX(2),'Xlim',[(min(x)-1) (max(x)+1)],...
'Ylim',[(min(y2)-1) (max(y2)+1)],'YTick', (min(y2)):5:(max(y2)))

get(AX(1))
get(AX(1),'Ylabel')
get(get(AX(1),'Ylabel'))

xlabel('LifeCycle Cost ($millions)','Fontsize',15);title('Non-dominated solutions for Total Nitrogen vs LifeCycle Cost','Fontsize',15)
% check out the result of '\musec' in the xlabel in the previous command


set(H1, 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red') % here H1 is plot landle
set(H2, 'MarkerEdgeColor', 'blue', 'MarkerFaceColor', 'none')

set(AX(1),'ycolor','r','lineWidth', 2) % y1 axis color
set(AX(2),'ycolor','b','lineWidth', 2) % y2 axis color

Steven Lord

unread,
Jan 30, 2015, 9:47:34 AM1/30/15
to


"Michael DI MATTEO" <mdimatt...@gmail.com> wrote in message
news:maeqf6$5kh$1...@newscl01ah.mathworks.com...
PLOTYY goes to some trouble to give you consistent tick locations. If you're
just going to undo that work, why not plot your data in two SUBPLOT axes,
like subplot(2, 1, 1) and subplot(2, 1, 2)?

ax1 = subplot(2, 1, 1);
scatter(x, y1);
ax2 = subplot(2, 1, 2);
scatter(x, y2);

If you need to force the two axes to have the exact same X axes:

linkaxes([ax1, ax2], 'x');

*snip*

Vivan Govender

unread,
Mar 17, 2015, 5:49:25 AM3/17/15
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <mag5hv$eau$1...@newscl01ah.mathworks.com>...
Although this solves the yyplot, has anyone managed to get the xxplot for a scatter plot working?
0 new messages