Vertical alignment of matplotlib legend labels with LaTeX math(matplotlib 图例标签与 LaTeX 数学的垂直对齐)
问题描述
将带有下标的标签与不带下标的标签混合在一起时,它们在图例中无法正确垂直对齐.由于 matplotlib 根据打印字符在内部确定边界框,因此使用 vphantom 字符无法对齐这些图例标签,并且我没有任何运气使用 set_va 更改标签的垂直对齐方式,要么.
When mixing labels that have subscripts with labels without them, they do not vertically align properly in the legend. Since matplotlib determines bounding boxes internally based on printing characters, using a vphantom character does not work to align these legend labels, and I have not had any luck changing the vertical alignment of the labels with set_va, either.
下面是一个 MWE,它说明了我要解决的问题.如果可能的话,我希望标签与文本基线对齐,否则与文本顶部对齐.
Below is a MWE that illustrates problem I am trying to solve. I would like the labels to align to the text baseline if at all possible, otherwise to the text top.
import numpy as np
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
x = np.arange(10)
plt.plot(x, np.random.uniform(size=(10,)), c='red', label=r'test')
plt.scatter(x, np.random.uniform(size=(10,)), c='blue', label=r'test${}_{xy}$')
plt.legend(ncol=2)
plt.show()
推荐答案
将 text.latex.preview 参数设置为 True:
import numpy as np
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preview'] = True
import matplotlib.pyplot as plt
x = np.arange(10)
plt.plot(x, np.random.uniform(size=(10,)), c='red', label=r'test')
plt.scatter(x, np.random.uniform(size=(10,)), c='blue', label=r'test${}_{xy}$')
plt.legend(ncol=2)
plt.show()
preview参数的效果,另请参考这个例子.
For the effect of the preview argument, also refer to this example.
这篇关于matplotlib 图例标签与 LaTeX 数学的垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:matplotlib 图例标签与 LaTeX 数学的垂直对齐
基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
