Plot with 2 X Axis. Y Axis blurry

8 views (last 30 days)
Hello,
so I've created a plot that contains two datasets. I've created 2 x-axis, which worked perfectly fine. But it appears that there've been created two y-axis which arent quite overlapping. THe datasets use the same axis, its just that one of the y-axis seems to be unachored. I#Ve been struggeling with fixing this. How could I either make those y-axis overlap or better delete one of them?
I hope you guys can help!
This is the result:
This would be my code:
figure
pl2 = plot(hub_vek_ges, mu,'Color',[0, 0.447, 0.741]);
hold on
pl1 = plot(hub_vek_MOH, mu_MOH,'Color',[0.635 , 0.078, 0.184]);
hold on
grid on
axis([0 40 0 0.14])
yticks([ 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14])
xticks([0:5:40])
xlabel('Hübe [-]', 'FontSize', 20);
ylabel('Reibkoeffizient {\it µ} [-]', 'FontSize', 20);
legend('MOH','geschliffen', 'FontSize', 20)
set(gca, 'fontname','times')
set(gcf,'color', 'white')
ax1 =gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
set(ax2,'XAxisLocation','top');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim')); %
set(ax2, 'XTick', get(ax1, 'XTick'), 'YTick', get(ax1, 'YTick')); %
axLabels = {'0' '250' '500' '750' '1000' '1250' '1500' '1750' '2000'};
set(ax2, 'XTickLabel', axLabels, 'YTickLabel', [ 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14] )
xlabel('Distanz {\it s} in mm', 'FontSize', 20)
set(gca, 'fontname','times')

Accepted Answer

Adam Danz
Adam Danz on 7 Jan 2021
Edited: Adam Danz on 7 Jan 2021
It looks like the y-axis limits do not match between both of the axes.
The only reason you're using two axes, as far as I can see, is to have two sets of x-tick-labels. Have you considered using just 1 x-y-axis and using text() or annotation() to label the upper x-tick labels? Or even easier, using two layers of x-tick labels at the bottom (see example)?
If you prefer using two axes, there's a safer way to do it. After setting up the first axis you can copy it, along with its position, using copyobj. Then use linkprop to link the two axes' properties so if one changes, the other changes in the same way. You should link xtick, ytick, xlim, ylim, DataAspectRatio, position, innerPosition, xdir, ydir, and any other property that might change for one of the axes. Then, after you visually inspect that the axis ticks are perfectly aligned, you can turn off the upper axis (axis(h,'off')) and make it inaccessible (h.HandleVisibility='off'; h.HitTest='off'; h.PickableParts='none';).
Here are some examples,

More Answers (0)

Community Treasure Hunt

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

Start Hunting!