figure legend (both text and colors) appears "faded". How can I fix it?
42 views (last 30 days)
Show older comments
Önundur Ragnarsson
on 22 Feb 2024
Answered: Abhinav Aravindan
on 28 Mar 2024
Hello everyone,
I am currently making a figure in Matlab, with both stacked areas and lines. For some reason the legend comes out with faded colors. Instead of black, the text in the legend appears light grey, and the colors which should be the same as in the plot area, are essentially the right colors, just in a much more faded shade (see attachment).
Below is the code for the figure. It already reflects my attempts at solving the problem, by creating invisible lines for the legend entries.
Anyone have an idea why it comes out this way?
Thanks in advance.
figure;
% Stack the areas
area(TimeAxis, newBuildingConfidence, 'FaceColor', 'flat', 'EdgeColor', 'none','BaseValue', baseValue);
colormap(rgb_colors);
hold on;
% Plot the main line (column 4, blue)
plot(TimeAxis, newBuildingCoeffs(:, 4), 'Color', [236 107 18]/255, 'LineWidth', 2.0);
% Plot the zero line (horizontal black solid line)
line([min(TimeAxis), max(TimeAxis)], [0, 0], 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1);
% Plot invisible lines for legend entries
h1 = plot(NaN, NaN, 'Color', [rgb_colors(2,:), 1], 'LineWidth', 10, 'Visible', 'off'); % Light blue for 68% confidence
h2 = plot(NaN, NaN, 'Color', [rgb_colors(3,:), 1], 'LineWidth', 10, 'Visible', 'off'); % Blue for 90% confidence
h3 = plot(NaN, NaN, 'Color', [rgb_colors(4,:), 1], 'LineWidth', 10, 'Visible', 'off'); % Dark blue for 95% Confidence
h4 = plot(NaN, NaN, 'Color', [[236 107 18]/255, 1], 'LineWidth', 2.0, 'Visible', 'off'); % Orange for Estimated premium
hold off;
title('Premium on new buildings with confidence bands');
xlabel('Time - Quarter in which rolling window ends');
ylabel('Coefficient (in logs)');
% Design the legend
lgd = legend([h1, h2, h3, h4], {'95% Confidence', '90% Confidence', '68% Confidence', 'Coefficient'}, 'Location', 'southoutside');
lgd.Orientation = 'horizontal';
lgd.NumColumns = 4;
lgd.TextColor = 'black'; % Set legend text color to black
1 Comment
Star Strider
on 22 Feb 2024
I opened and displayed it to see what the problem is. I have never seen this behaviour, and legend objects do not appear to have any relevant properties (such as 'Layer' or any sort of transparency property) that could cause that.
I suggest that you Contact Support and ask them. Include the URL of this thread so that you do not have to repeat everything here, and they have access to the figure as well.
F1 = openfig('Premium.fig');
hl = findobj(F1','Type','legend');
get(hl)
Accepted Answer
Abhinav Aravindan
on 28 Mar 2024
The faded legend appears to be an expected behaviour where the legend entry dims if the visibility of its corresponding line is set to "off". A workaround for this is to remove the “Visible”, “off” Name-Value pair from “h1”, “h2”, “h3” and “h4” in your code.
The following code snippet illustrates the behaviour of the legend when altering the line visibility.
% Create Figure
figure
% Sample data to plot
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x/2);
% Plot NaN lines with "Visible" = "off"
subplot(2,1,1)
plot(x,y1)
hold on
plot(x, y2)
h1 = plot(NaN, NaN, 'LineWidth', 10, 'Visible', 'off');
h2 = plot(NaN, NaN, 'LineWidth', 10, 'Visible', 'off');
lgd1 = legend([h1, h2], {'Plot 1', 'Plot 2'});
title("NaN lines with 'Visible' = 'off'")
hold off
% Plot NaN lines with "Visible" = "on"
subplot(2,1,2)
plot(x,y1)
hold on
plot(x, y2)
h1 = plot(NaN, NaN, 'LineWidth', 10);
h2 = plot(NaN, NaN, 'LineWidth', 10);
lgd2 = legend([h1, h2], {'Plot 1', 'Plot 2'});
title("NaN lines with 'Visible' = 'on'")
hold off
Output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1654121/image.png)
A similar query to yours has been resolved here:
For further information about legend properties, please refer to the links below:
0 Comments
More Answers (1)
Zinea
on 28 Mar 2024
Hi Önundur,
The legend entries appear faded because the ‘Visible’ property is set to ‘off’ for the variables ‘h1’ to ‘h4’. Changing them to ‘on’ solves the issue. Precisely,
h1 = plot(NaN, NaN, 'Color', [rgb_colors(2,:), 1], 'LineWidth', 10, 'Visible', 'on'); % Light blue for 68% confidence
Repeat for h2 , h3 and h4. I have attached the screenshot of the plot after the changes.
Hope it helps!
Zinea
0 Comments
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!