Filter löschen
Filter löschen

Getting unwanted box in the legend

3 Ansichten (letzte 30 Tage)
Captain Rituraj Singh
Captain Rituraj Singh am 28 Feb. 2024
Kommentiert: Walter Roberson am 4 Mär. 2024
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')

Antworten (1)

Walter Roberson
Walter Roberson am 28 Feb. 2024
Verschoben: Walter Roberson am 28 Feb. 2024
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 Kommentare
Walter Roberson
Walter Roberson am 4 Mär. 2024
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 am 4 Mär. 2024
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)

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by