简体   繁体   English

如何在matplotlib图中删除LaTeX风格数学中的奇怪空间

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

I am using the following as an axis label in matplotlib: 我在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. 但是当我生成一个图形作为y轴标签时,我得到下面的图像,它在m ^ 2和\\ mu之间有一个奇怪的空间,这使得它看起来很奇怪。 How can I remove this strange space? 我怎样才能删除这个奇怪的空间?

示例图

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$)")

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 . 顺便说一下,我建议使用带有LaTeX公式的原始字符串,因为这会阻止LaTeX命令(或其中的一部分)被解释为反斜杠转义:你可能不希望\\tau结尾作为选项卡后跟au

Matplotlib tries to interpret Latex code and has its own method of printing the text. Matplotlib试图解释Latex代码并有自己的打印文本的方法。 The results do not necessarily match the output of the real Latex compiler. 结果不一定与真正的Latex编译器的输出匹配。 If you want real Latex text rendering, you must enable the text.usetex rc parameter. 如果要进行真正的Latex文本渲染,则必须启用text.usetex rc参数。 This also allows you to load Latex packages (like siunitx) and to create figures that match your document if you are planning to include them. 这也允许您加载Latex包(如siunitx)并创建与您的文档匹配的数字(如果您计划包含它们)。

The extra space does not appear when using Latex. 使用Latex时不会出现额外的空间。 You could just use the workaround and add a negative space, but I tend to use Latex when creating figures for documents. 您可以使用变通方法并添加负空格,但我倾向于在创建文档数字时使用Latex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM