Superposition of two axes on Matlab

I am looking for a way to superimpose the xy time series, for example, created using "plot", on top of the display generated by "contourf", with different scaling along the y axis.

It seems that a typical way to do this in the case of two xy graphs is to use the built-in "plotyy" function, which can even be controlled by functions other than "plot" (for example, "loglog") as long as the input arguments remain unchanged (x, y). However, since contourf requires three input arguments in my case, "plotyy" does not seem to be applicable. Here is a sample code that describes what I would like to do:

x1 = 1:1:50; y1 = 1:1:10; temp_data = rand(10,50); y2 = rand(50,1)*20; figure; hold on; contourf(x1,y1,temp_data); colormap('gray'); plot(x1,y2,'r-'); 

Ideally, I would like the time series (x1, y2) to have their own y-axis, displayed on the right, and scale to the same vertical extent as the contour graph.

Thank you for your time.

+8
matlab plot
source share
1 answer

I don’t think there is a β€œclean” way to do this, but you can fake it by laying two axes on top of each other.

 x1 = 1:1:50; y1 = 1:1:10; temp_data = rand(10,50); y2 = rand(50,1)*20; figure; contourf(x1, y1, temp_data); colormap('gray'); h_ax = gca; h_ax_line = axes('position', get(h_ax, 'position')); % Create a new axes in the same position as the first one, overlaid on top plot(x1,y2,'r-'); set(h_ax_line, 'YAxisLocation', 'right', 'xlim', get(h_ax, 'xlim'), 'color', 'none'); % Put the new axes' y labels on the right, set the x limits the same as the original axes', and make the background transparent ylabel(h_ax, 'Contour y-values'); ylabel(h_ax_line, 'Line y-values'); 

In fact, this β€œplot overlay” is almost what plotyy internal function plotyy .

Here's an example output (I increased the font size for readability): overlaid axes

+6
source share

All Articles