使用乳胶符号作为图例标签的右箭头(
\rightarrow
),并在matplotlib
中使用内部乳胶渲染,调整标签条目之间的行距;请参阅下面的最小示例,它创建了两个附加的 png 图形。有没有办法在使用乳胶符号时保持统一的标签间距(labelspacing
的参数plt.legend()
似乎不适用于此)?我知道对于高符号或分数等行距应该更宽,但箭头实际上比文本字符小。
规格:python 3.7.6,matplotlib 3.1.2
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.arange(10)
def plot_example(plot_case):
plt.plot(x, y, label=r'test case a1')
plt.plot(x, y, label=r'test case b1')
if plot_case == 'good':
plt.plot(x, y, label=r'test case c1')
plt.plot(x, y, label=r'test case d1')
plt.plot(x, y, label=r'test case e1')
elif plot_case == 'bad':
plt.plot(x, y, label=r'test case c1 $\rightarrow$ c2')
plt.plot(x, y, label=r'test case d1 $\rightarrow$ d2')
plt.plot(x, y, label=r'test case e1 $\rightarrow$ e2')
handles, labels = plt.gca().get_legend_handles_labels()
plt.legend(handles, labels, fontsize=7)
plt.savefig('~/test_legend_labelspacing_latex_arrow_%s.png' % plot_case)
plt.close()
plot_example('good')
plot_example('bad')
解决办法可以
plt.legend(handles, labels, fontsize=7, handleheight=2.5)