Plotyy Display Gridlines Axis
24 ビュー (過去 30 日間)
古いコメントを表示
I am using plotyy, I managed to show the grid for the function on the left using 'grid on', but how do I show the gridlines using the function on the right?
1 件のコメント
DadPunsAreBadPuns
2020 年 7 月 17 日
Whyyaxis question? Because you wanted an answer!
Since plotyy is no longer recommended, here's a workaround with yyaxis. You can make it into your own %<UserProfile>%\Documents\MATLAB\ folder for future use.
close all; clear all; clc;
% Setup dummy data
x = 1:10;
y = x;
% Setup colors
line_color = [ 1 , 0 , 0 ]; % Red
alpha_value = 0.15;
grid_line_color = [ line_color , alpha_value ]; % Transparent red
% Create a figure and plot the dummy-data line
fig1 = figure(1);
ax1 = axes;
hold on;
yyaxis right;
line1 = plot( x , y , '-' , 'Color' , line_color );
myYGridHandles = plotRightYGrid( ax1 , grid_line_color );
function [ gridLineHandles ] = plotRightYGrid( axh , grid_line_color )
% Made into a function if you'd like to reuse in the future.
% The following is possibly redundant, but performed for sanity's sake
axes( axh );
yyaxis right;
% Acquire the x-axis limits to get the grid-lines' extents
xlim_right = xlim;
% Acquire the tick mark values; these can be default or specified by user
y_ticks_right = get( gca , 'YTick' );
% Plot the major Y-right grid lines
for( j = 2 : ( length( y_ticks_right ) - 1 ) )
gridLineHandles(j) = plot( [ xlim_right(1) , xlim_right(2) ] , ...
[ y_ticks_right(j) , y_ticks_right(j) ] , ...
'-' , 'Color' , grid_line_color );
end
end
採用された回答
Jonathan LeSage
2013 年 10 月 17 日
編集済み: Jonathan LeSage
2013 年 10 月 17 日
You can select the second axis handle for the data plotted with respect to the right y-axis. If you define an output for the plotyy function, the output variable will contain the figure axes handles in a vector. You can then use the set function to define the 'Xgrid' and 'Ygrid' states as 'on'.
For example:
% Outputs of the plotyy are the axes handles (in a vector) and each line handle
[haxes,hline1,hline2] = plotyy(t,z1,t,z2,'semilogy','plot');
% Turn the grid for the second axis on
set(haxes(2),'Xgrid','on')
set(haxes(2),'Ygrid','on')
Check out the documentation for more information on the set function:
2 件のコメント
James Ryan
2018 年 2 月 26 日
plotyy is no longer recommended in the docs, which now recommend yyaxis. Is there an updated way to accomplish this with yyaxis, which has no output?
Thanks! By the way, I'm not complaining about the original answer. I assume it was good at the time.
Cris LaPierre
2018 年 12 月 19 日
It does not appear to be possible to add a grid line to the right axis when plotting using yyaxis.
Grid Lines
Grid lines correspond with the tick mark locations along the left y-axis.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Two y-axis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!