![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358993/image.png)
Match the y-axis color with legend color in multiple y axis plot?
6 views (last 30 days)
Show older comments
In this plot, how can I change the black y-axis scale color of Z (-10 60) to be red as seen in the legend?
I have used color r in line 4 but I got black in return.
figure('position',[100 100 800 500])
hold on
% Plot the first set of data using the main y-axis
plot(range, range_dbz{tt,4}(:,4),'r', 'LineWidth', 1.0)
addaxis(range, range_zdr{tt,4}(:,4),[-2 8],'b', 'LineWidth', 1.0)
addaxis(range, range_rho{tt,4}(:,4),[0.5 1.0], 'k','LineWidth', 1.0)
set(gca,'xlim', [40 80])
ylabel('Z (dBZ)','FontSize',12)
xlabel('Range (km)', 'FontSize',12)
line=xline([50 52.57 75],'--k','LineWidth',1.0);
% Plot the second set of data using the second y-axis
%addaxis(range, range_zdr{tt,1}, [-2 8],'b', 'LineWidth', 1.5)
addaxislabel(2, 'Zdr (dB)','FontSize',12)
% Plot the third set of data using the third y-axis
%addaxis(range, range_rho{tt,1}, [0.5 1.0], 'r','LineWidth', 1.5)
addaxislabel(3, 'Rhohv','FontSize',13)
legend('Z','Zdr','Rhohv', 'Location','northeast','FontSize',7)
title(['Azimuth: ',num2str(desired_azimuth),'^o', ' | ', 'Elevation: 3.5','^o',' | ', '12:14:26 UTC',] ,...
'Fontsize',15,'FontWeight','normal','color','k');
grid on
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358943/image.png)
0 Comments
Accepted Answer
DGM
on 17 Apr 2023
The ruler color can be set using 'ycolor'. When you use legend(), explicitly give it a vector of object handles as the first argument.
For both these tasks, you're going to have to keep track of the objects you're creating. You can use getaddaxisdata() to get axes handles, or you can use findobj(), etc. All plot (line) objects are in the first axes that was created with the first plot() command. The other axes objects only serve to host extra ruler objects.
% fake data
x = 1:10;
y1 = rand(1,10);
y2 = rand(1,10);
y3 = rand(1,10);
hold on
% Plot the first set of data using the main y-axis
plot(x, y1,'r', 'LineWidth', 1.0)
% Plot the second set of data using the second y-axis
addaxis(x, y2,'b', 'LineWidth', 1.0)
% Plot the third set of data using the third y-axis
addaxis(x, y3, 'k','LineWidth', 1.0)
% add a constantline or two or three
line = xline(5,'--k','LineWidth',1.0);
% set axis labels
addaxislabel(2, 'Zdr (dB)','FontSize',12)
addaxislabel(3, 'Rhohv','FontSize',13)
% get axes handles
hax = getaddaxisdata(gca,'axisdata');
hax = [hax{:}];
hax = [gca hax(1,:)];
% only get line objects in the base axes
% flip the handle vector, since they're in order of ascending age
h = flipud(findobj(hax(1),'type','line'));
legend(h,{'Z','Zdr','Rhohv'}, 'Location','northeast','FontSize',7)
% set the color of the yruler for the base axes
hax(1).YColor = h(1).Color;
hax(1).FontSize = hax(2).FontSize;
grid on
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358993/image.png)
See also:
0 Comments
More Answers (0)
See Also
Categories
Find more on Legend 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!