Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
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.

Respuestas (2)

Venkata Siva Krishna Madala
Venkata Siva Krishna Madala el 19 de Mzo. de 2018
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 el 26 de Ag. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by