필터 지우기
필터 지우기

figure legend (both text and colors) appears "faded". How can I fix it?

조회 수: 57 (최근 30일)
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
Star Strider
Star Strider 2024년 2월 22일
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)
AutoUpdate: on BeingDeleted: off Box: on BusyAction: 'queue' ButtonDownFcn: @bdowncb Children: [0×0 GraphicsPlaceholder] Color: [1 1 1] ContextMenu: [1×1 ContextMenu] CreateFcn: '' DeleteFcn: '' Direction: 'normal' EdgeColor: [0.1500 0.1500 0.1500] FontAngle: 'normal' FontName: 'Helvetica' FontSize: 9 FontWeight: 'normal' HandleVisibility: 'on' HitTest: on Interpreter: 'tex' Interruptible: off ItemHitFcn: @defaultItemHitCallback Layout: [0×0 matlab.ui.layout.LayoutOptions] LineWidth: 0.5000 Location: 'southoutside' NumColumns: 4 NumColumnsMode: 'manual' Orientation: 'horizontal' Parent: [1×1 Figure] PickableParts: 'visible' Position: [0.0458 0.0971 0.8597 0.0297] Selected: off SelectionHighlight: on String: {'95% Confidence' '90% Confidence' '68% Confidence' 'Coefficient'} Tag: 'legend' TextColor: [0 0 0] Title: [1×1 Text] Type: 'legend' Units: 'normalized' UserData: [] Visible: on

댓글을 달려면 로그인하십시오.

채택된 답변

Abhinav Aravindan
Abhinav Aravindan 2024년 3월 28일
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:
A similar query to yours has been resolved here:
For further information about legend properties, please refer to the links below:

추가 답변 (1개)

Zinea
Zinea 2024년 3월 28일
Hi Önundur,
The legend entries appear faded because the ‘Visibleproperty 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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by