How do I change location of the x axis of two scatter plots. I want x1 axis up and x2 axis down and y on the left of the plot

10 views (last 30 days)
Hi all, Thank you in advance, I have to plot two scatter plots with different x axis values. I want the x axis values positioned up and down the plot and the y axis on the left as usual. I know in the normal plot function, changing the axis position is possible in the plot properties. However, I checked the scatter plot properties in the doc and it appears there is no such properties as axis position. When I try to find a way around this by using hold on, the first plot seems to be written over by the second. I need you assistance please. Here is my MWE
y = [0:20];
x1 = rand(1,length(y));
x2 = rand(1,length(y));
scatter(gca,x1,y,'MarkerFaceColor',[0 .7 .7],...
'LineWidth',1.5)
xlabel('x1');
ylabel('y');
set(gca,'XAxisLocation','bottom')
ax1 = gca;
set(ax1,'XAxisLocation','top')
hold on ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','top','XColor','k','YColor','k'); scatter(ax2,x2,y,'MarkerFaceColor',[0 1 0],... 'LineWidth',1.5) xlabel('x2'); ylabel('y'); hold off
Thank you.

Answers (2)

Venkata Siva Krishna Madala
Hello Lewis Asilevi,
I understand your issue but after trying for a long time I have realised that there is no direct way of doing it. The only workaround I found is to do it this way :-
y = [0:20];
x1 = rand(1,length(y));
x2 = rand(1,length(y));
line(x1,y,'LineStyle','none','Marker','*')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','Color','none');
line(x2,y,'Parent',ax2,'LineStyle','none','Marker','o')
Regards,
Krishna Madala

hadi eidinejad
hadi eidinejad on 26 Aug 2022

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!