How to make the negative sign in the legend easier to see?

17 visualizaciones (últimos 30 días)
Daigo
Daigo el 20 de Oct. de 2021
Comentada: Daigo el 21 de Oct. de 2021
I generated the following legend in my plot but the negative sign ('-') is hard to see. I tried several different fonts but it didn't make a big difference. Is there any way to make it look better? For example, I'd like to make the negative sign "longer" and also get some space between "=" sign and the numbers.
The corresponding part of my code looks like this:
for idx = 1:length(sigma_d_list)
sigma_d = sigma_d_list(idx)
plot(x, y); hold on;
Legends{idx} = strcat('\delta_{d} = ', ' ', num2str(sigma_d*1e3), ' mm');
end
xlabel('u (m)'); ylabel('PSF');
xlim([-0.5 0.5]*1e-4);
lgnd = legend(Legends);
set(lgnd, 'FontSize', 14);
set(lgnd, 'FontName', 'Times New Roman');

Respuesta aceptada

Jan
Jan el 20 de Oct. de 2021
Editada: Jan el 20 de Oct. de 2021
strcat removes interior spaces. This is not useful here and it is not in general. I consider this as a design error.
Use cat instead:
Legends{idx} = cat(2, '\delta_{d} = ', ' ', num2str(sigma_d*1e3), ' mm');
% or
Legends{idx} = ['\delta_{d} = ', ' ', num2str(sigma_d*1e3), ' mm'];
Do you really want 3 spaces? Or was this a try to struggle with the smart deblanking of strcat?
I'm not a fan of num2str also, which calls sprintf internally. Just call it directly:
Legends{idx} = sprintf('\\delta_{d} = %g mm', sigma_d * 1e3);
Nicer, faster and less confusing.
  5 comentarios
Daigo
Daigo el 21 de Oct. de 2021
@Jan Thanks Jan! That looks perfect!
Daigo
Daigo el 21 de Oct. de 2021
@Bruno Luong Aha, you can set such a fine-grained font format! Thanks for your tip!

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 20 de Oct. de 2021
You can get some more space by using cell arrays in strcat (because strcat trims white space off the ends of character arrays):
Legends{idx} = strcat({'\delta_{d} = '}, {' '}, num2str(sigma_d*1e3), ' mm');

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by