MATLAB Answers

giutri
0

Multiple axes: how to plot the correct scale on both x-axes and y-axes?

Asked by giutri
on 18 Jul 2019 at 16:42
Latest activity Commented on by Adam Danz
on 23 Jul 2019 at 14:33
Hello,
CAn I ask you some advice for these two problems? I am using Matlab very ofte.. Apologies for any mistakes.
I am using a variogramfit to run a semivariogram analysis of three datasets (they different in length and magnitude). I would plot the result in one graph with two y-axes and 2 x-axes. I was able to use the instructions for multiple axes but I realised that I got some weird result. On the second y-axes and x-axes I have got double ticks that possibly reflect two different scale? I am not sure how I can proceed. Any suggestions?
I would select three different colour for each datasets, but I am not sure how to do it. I have tried to change the color after plotting, but it is not possible.
figure(1)
[a1,c1,n1] = variogramfit(dist_uv_F,gamma_uv_F,a01_F,c01_F,[],...
'solver','fminsearchbnd',...
'nugget',0,...
'plotit',true)
hold on
[a1,c1,n1] = variogramfit(dist_uvV,gamma_uvV,a01_V,c01_V,[],...
'solver','fminsearchbnd',...
'nugget',0,...
'plotit',true)
hold on
ylabel('gamma (uv)')
xlabel('lag distance dataset1-2')
hold on
ax1 = gca;
set(ax1, 'XColor','k', 'YColor','k')
ax2 = axes('Position', get(ax1,'Position'),...
'XAxisLocation', 'top',...
'YAxisLocation', 'right',...
'Color','none',...
'XColor', 'k', 'YColor', 'k');
hold on
h = variogramfit(dist_uv,gamma_uv,a01,c01,[],...
'solver','fminsearchbnd',...
'nugget',0,...
'plotit',true)
hold on
xlabel('lag distance dataset3')

  0 Comments

Sign in to comment.

1 Answer

Answer by Adam Danz
on 18 Jul 2019 at 16:55
Edited by Adam Danz
on 19 Jul 2019 at 12:29
 Accepted Answer

The double ticks are due to the axis 'Box" property set to 'on' and the misalignment of ticks between the upper and lower axes (or left and right axes). There are two ways around this.
1) If you want the keep the 'box' 'on', scale the pair of axes (upper / lower or left /right) so they have the same number of ticks at equal spacing.
  • use xlim() or ylim() to set the axis limits
  • use set(h, 'xtick', ...) or set(h, 'ytick',...) to set the ticks
2) Turn off the axis box for both axes
  • box(ax1,'off')
  • box(ax2,'off')
Turning the grid on for both axes may help visualize the problem.
  • grid(ax1,'on')
  • grid(ax2,'on')

  2 Comments

Thanks! Now it worked!
Can I also ask some suggestions to modify the markers in the script? I would have different markers for different dataset but unfortunately I am not able to modify them after the plot.
I'm not familiar with the 3rd party function "variogramfit". To plot with different markers, you could use gscatter() or you could plot the groups individually.

Sign in to comment.