two x-axis plot

714 vues (au cours des 30 derniers jours)
Ham Man
Ham Man le 18 Fév 2023
Modifié(e) : dpb le 21 Fév 2023
I want to plot with two x-axis on top and bottom and also set the x values of both x-axis in the same vertical line:
for example: 500 of x1 be in the same line with 0.1, 850 be in the same line with 0.2 and ....
I wrote this code, but y values are not coincident for x1 and x2 and plots y values in two vertical lines beside each other.
x1 = [500 850 1200];
x2 = [0.1 0.2 0.3];
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1(3),20,'o','r'); hold on
Error using plot
Invalid data argument.
plot(ax1,x1(2),25,'o','b'); hold on
plot(ax1,x1(1),30,'o','k'); hold off
ylim([0 100]);
grid on; grid minor; ytickformat('percentage');
%========
ax2 = axes(t);
plot(ax2,x2(3),20,'o','r'); hold on
plot(ax2,x2(2),25,'o','b'); hold on
plot(ax2,x2(1),30,'o','k'); hold off
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
ylim([0 100]);
grid on; grid minor; ytickformat('percentage');

Réponses (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 18 Fév 2023
Here is a corrected code:
x1 = [500 850 1200];
x2 = [0.1 0.2 0.3];
t = tiledlayout(1,1);
ax1 = axes(t);
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right','color','none');
hold(hAx(2),'on')
plot(hAx(2),x1(3),20, 'ro', 'MarkerFaceColor', 'c'), hold on
plot(hAx(2),x1(2),25, 'bo', 'MarkerFaceColor', 'y'),
plot(hAx(2),x1(1),30, 'ko', 'MarkerFaceColor', 'g'),
hold off
ylim([0 100]);
grid on; grid minor; ytickformat('percentage');
ax1.Box = 'off';
%========
t = tiledlayout(2,1);
ax1 = axes(t);
GAx(1)=gca;
GAx(2)=axes('Position',GAx(1).Position,'XAxisLocation','top','YAxisLocation','right','color','none');
hold(GAx(2),'on')
plot(GAx(2),x2(3),20, 'ro', 'MarkerFaceColor', 'c'), hold on
plot(GAx(2),x2(2),25, 'bo', 'MarkerFaceColor', 'y'),
plot(GAx(2),x2(1),30, 'ko', 'MarkerFaceColor', 'g'),
hold off
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax2.Box = 'off';
ylim([0 100]);
grid on; grid minor; ytickformat('percentage');
  1 commentaire
Ham Man
Ham Man le 19 Fév 2023
Modifié(e) : Ham Man le 19 Fév 2023
Thanks for replying.
In the first plot, points does not match the x2 values!!
In the 2nd plot, I don't see the x1 values!!

Connectez-vous pour commenter.


dpb
dpb le 18 Fév 2023
Modifié(e) : dpb le 19 Fév 2023
Without all the niceties, but to illustrate putting the three points on two axes and set ticks such that grid lines will match up --
x1 = [500 850 1200];
x2 = [0.1 0.2 0.3];
y1=[30:-5:20]; y2=y1;
hSc=scatter(x1,y1); % the first scatter plot
hold on
hAx=gca; % save first axes handle..
hAx(2)=axes('position',hAx(1).Position, ... % create the second axis, top/right...
'color','none', ...
'XAxisLocation','top', ...
'YAxisLocation','right');
ylim(hAx,[0 100]) % and set the y range both
hold on
hSc(2)=scatter(hAx(2),x2,y2); % now add the second scatter plot...
xlm=xlim; % get the axis limits
xticks(linspace(xlm(1),xlm(2),numel(hAx(1).XTick))) % to scale same number ticks as other axes
hAx(2).XAxis.TickLabelFormat='%0.3f'; % make uniform format for looks...
grid on; % turn on major grid; observe aligned
set(hSc,{'MarkerFaceColor'},{'b'}) % make the markers more visible...
Can set the individual color vectors and all to suit...but above aligns up the ticks; if the x values aren't quite so nice in real life may have to set arbitrary xlim values so can adjust them to be at same relative position.
ADDENDUM: Addresses follow on Q? in Comment --
FACTOR=4; % fraction of tick mark distance to add/subtract
for h=hAx
dtk=mean(diff(xticks(h))); % difference between tick marks each axis
xlim(h,xlim(h)+[-1 1]*dtk/FACTOR) % adjust each by that fraction
end
As with the first, the trick is to do everything in scale; the xlim property is in absolute axis units so to keep things even you must use ratios -- the above is an idiom I've used before; you pick the fraction of the width between the tick marks you want for the extra space. This works well for axes as long as the ticks are reasonably well spaced; you can foul it up if only set one or two tick marks at odd locations as can happen for special effects.
For the special case as here of aligning the two, then the spacing will have to be uniform; otherwise one has to revert to mucking with the two xlim vectors themselves
  3 commentaires
Ham Man
Ham Man le 19 Fév 2023
Also how can I add legend?
dpb
dpb le 19 Fév 2023
Modifié(e) : dpb le 21 Fév 2023
hLg=legend(hSc,'a','b');
You'll have to decide just what it is you want shown differently; it's peculiar to have two separate plots with identically the same data on both arranged so they overlap identically, so what it means and how to annotate it will have to rely on you knowing what it is you're trying to show.
I just did the same color so they would show up; you'll have to fix up the other details as want/need...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by