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 次查看(过去 30 天)
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.

回答(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 2022-8-26

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by