Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove strange space in LaTeX-style maths in matplotlib plot

I am using the following as an axis label in matplotlib:

"Pixel Radiance ($W/m^2/\mu m$)"

But when I produce a graph with this as the y-axis label I get the image below, which has a strange space between the m^2 and the \mu which makes it look rather strange. How can I remove this strange space?

example graph

A reproducible example, without using any of my own data, is:

from matplotlib.pyplot import *
plot([1, 2, 3], [1, 2, 3])
ylabel("($W/{m^2}/\mu m$)")
like image 392
robintw Avatar asked Jul 29 '12 20:07

robintw


People also ask

How do I get rid of extra space in Matplotlib?

You can use either plt. margins(x=0) or ax. margins(x=0) to remove all margins on the x-axis.

How do I save a Matplotlib plot without white space?

Hide the Whitespaces and Borders in Matplotlib Figure To get rid of whitespace around the border, we can set bbox_inches='tight' in the savefig() method. Similarly, to remove the white border around the image while we set pad_inches = 0 in the savefig() method.

How do I change the plot style in Matplotlib?

Another way to change the visual appearance of plots is to set the rcParams in a so-called style sheet and import that style sheet with matplotlib. style. use . In this way you can switch easily between different styles by simply changing the imported style sheet.


1 Answers

You can use a negative space, \!:

r"Pixel Radiance ($W/m^2\!/\mu m$)"

Incidentally, I'd recommend using raw-strings with LaTeX formulae, as that will prevent LaTeX commands (or parts of them) being interpreted as backslash-escapes: you probably wouldn't want \tau ending up as a tab followed by au.

like image 59
Luke Woodward Avatar answered Oct 14 '22 04:10

Luke Woodward