如何将文本添加到matplotlib图例并使用图例和绘图边框保持相等的空间?

时间:2015-03-17 09:58:21

标签: python matplotlib textbox

我正在尝试将文本添加到matplotlib图例中,可以通过添加注释单独完成,但我需要将它们放在一个框中,这就是我所做的:

import numpy as np
import matplotlib.pyplot as plt
A = np.array([1, 2, 3, 4, 5, 6,])
B = A ** 2
C = A ** 3
fig = plt.figure(1, figsize=(5, 5))
fig.clf()
plot = plt.subplot(111)
plot.plot(A, B, linestyle = '-', marker='^', label='quadratic')
plot.plot(A, C, linestyle = '-', marker='^', label='cubic')
plot.legend(title = 'Lorem ipsum \n' + str(A[1]) + ' dolor sit amet,\nte dolores assentior \ninciderint iu', loc = 'center', bbox_to_anchor = (1.25, 0.5))
plot.set_ylabel('y')
plot.set_xlabel('x')
plt.savefig('test.pdf', bbox_inches='tight')

同时我需要绘图和图例边界之间的间距相同,因为图例宽度增加,图例应该放在不改变loc参数的位置,如图S所示应该是常量:

+------------------------------------------------------+
|                                                      |
|                                                      |
|                                                      |     +----------+
|                                                      |     |          |
|                                                      |  S  |          |
|                                                      |<--->|          |
|                                                      |     |          |
|                                                      |     |          |
|                                                      |     |          |
|                                                      |     +----------+
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
+------------------------------------------------------+

是否有更简单的方法在图例中添加文本,并使用图例框中的文本对齐选项保持间距不变等等。

0 个答案:

没有答案