Question About Plotting Using Multiple Y-Axes in Same Plot

2 views (last 30 days)
I am preparing a figure for a scientific publication that will use two y-axes. However, when trying to plot using yyaxis left and yyaxis right, it seems like yyaxis left the primary axis which goes on the plot first followed by what is specified by yyaxis right which is probably the secondary axis. When this happens, graphics targeting the right y-axis cover the more important graphics associated with the left y-axis. An example of what I talking about is attached.
The top plot in the example figure has pH on the left and salinity on the right which is the configuration I want, but the shaded area associated with salinity covers the profile of pH. The bottom figure has things reversed with salinity on the left and the other parameters on the right produces the effect I want with the profiles transposed on top of the shaded region associated with salinity. I tried putting yyaxis right ahead of yyaxis left in the script to try and change the order in which graphics are plotted, but it did not work. Is there any way in MATLAB to have the graphics associated with yyaxis right plot first and the graphics associated with yyaxis left plot second (as this would produce the desired configuration and effect I am looking for)? Thanks in advance for the help.

Answers (1)

Walter Roberson
Walter Roberson on 14 Dec 2017
Perhaps something from the discussion https://blogs.mathworks.com/graphics/2014/11/04/sortmethod/ will help.
But if not, then change the z coordinates of one of the two portions. One way to do that would be to parent a hgtransform to the axes and parent all of the objects normally drawn in the axes into the hgtransform, and then set the transform matrix to move the z coordinates slightly below 0.
  1 Comment
James Barabas
James Barabas on 2 Jan 2018
Yes, this should work. For example, if your original plot was created like this:
yyaxis left
plot(10:-1:1)
yyaxis right
area(1:10)
then you can add an hgtransform to the left side that moves graphics to higher z-values than the graphics on the right.
yyaxis left
zoffset = 1;
t = hgtransform('Matrix',makehgtform('translate',0,0,zoffset));
plot(t,10:-1:1)
yyaxis right
area(1:10)
set(gca,'SortMethod','depth')

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!