Getting unwanted box in the legend

3 次查看(过去 30 天)
Hi,
I am getting an unwanted box type legend symbol as can be seen in the attached plot. Please help me to get rid-off this thing.
Here is my code for the plotting.
clear all
% Load data for the first data set
infile = 'JGD200z0.0.txt';
data1 = load(infile, '-ascii');
gd1 = data1(:, 1);
pt1 = data1(:, 3);
teff1 = data1(:, 2);
% Load data for the second data set
infile = 'JGD200z0.2.txt';
data2 = load(infile, '-ascii');
gd2 = data2(:, 1);
pt2 = data2(:, 3);
% Load data for the third data set
infile = 'JGD200z0.4.txt';
data3 = load(infile, '-ascii');
gd3 = data3(:, 1);
pt3 = data3(:, 3);
teff3 = data3(:, 2);
% Create a figure
figure;
% Create a 2D surface plot for the first data set
h1 = surf([pt1'; pt1'], [gd1'; gd1'], [teff1'; teff1'], 'EdgeColor', 'interp', 'FaceColor', 'none', 'LineWidth', 3.5, 'LineStyle','--');
hold on;
% Create a 2D surface plot for the second data set
h2 = surf([pt2'; pt2'], [gd2'; gd2'], [teff1'; teff1'], 'EdgeColor', 'interp', 'FaceColor', 'none', 'LineWidth', 3.5, 'LineStyle','-.','marker','o');
% Create a 2D surface plot for the third data set
h3 = surf([pt3'; pt3'], [gd3'; gd3'], [teff3'; teff3'], 'EdgeColor', 'interp', 'FaceColor', 'none', 'LineWidth', 3.5);
% Set the view to have a 2D appearance
view(2);
% Add colorbar
c = colorbar;
ylabel(c, '\bf \fontsize{18}{0}\selectfont $\mathbf T_{eff}$ [GeV]', 'Interpreter', 'latex');
% Set legend
legend([h1, h2, h3], '\zeta = 0.0', '\zeta = 0.2', '\zeta = 0.4');
set(legend, 'FontSize', 25, 'FontWeight', 'bold');
grid on;
title('T = 0.200 GeV','$\mathbf J/\psi$', 'BackgroundColor', [1 1 1], 'LineWidth', 2.5, 'EdgeColor', [0 0 0], 'FontSize', 40, 'Interpreter', 'latex');
ylabel('\bf \fontsize{27}{0}\selectfont $\mathbf \Gamma_{D}$ [GeV]', 'Interpreter', 'latex');
xlabel('\bf \fontsize{27}{0}\selectfont $\mathbf p_{T}$ [GeV]', 'Margin', 4, 'HorizontalAlignment', 'center', 'Interpreter', 'latex', 'VerticalAlignment', 'middle');
ax = gca;
ax.LineWidth = 2.5;
ax.TickLength = [0.02 0.0095];
set(gca, 'XMinorTick', 'on', 'YMinorTick', 'on')

回答(1 个)

Walter Roberson
Walter Roberson 2024-2-28
移动:Walter Roberson 2024-2-28
The boxes in the legend are because you are using surf()
You could create dummy lines,
H(1) = plot(nan, nan, 'LineStyle', '--', 'displayname', '\zeta = 0.0');
H(2) = plot(nan, nan, 'LineStyle', '-.', 'displayname', '\zeta = 0.2');
H(2) = plot(nan, nan, 'LineStyle', '-', 'displayname', '\zeta = 0.4');
legend(H, 'show')
  3 个评论
Walter Roberson
Walter Roberson 2024-3-4
If you replace your current lengend call with
legend(H, 'show')
then you will only legend() the regular (dummy) lines, without the legend showing the boxes,
Walter Roberson
Walter Roberson 2024-3-4
h1 = surf(rand(3,5), rand(3,5), rand(3,5));
hold on
h2 = surf(rand(3,5), rand(3,5), rand(3,5));
h3 = surf(rand(3,5), rand(3,5), rand(3,5));
H(1) = plot(nan, nan, 'LineStyle', '--', 'displayname', '\zeta = 0.0');
H(2) = plot(nan, nan, 'LineStyle', '-.', 'displayname', '\zeta = 0.2');
H(3) = plot(nan, nan, 'LineStyle', '-', 'displayname', '\zeta = 0.4');
legend(H)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by