Matplotlib的传说太宽了

时间:2017-09-18 17:13:59

标签: python matplotlib

我正在绘制一条包含六条曲线的图形,其中每条曲线都有一个标签。图例位于图表下方,但它比图更宽。请参阅代码和屏幕截图。

#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(6):
    ax.plot(x, i * x, label='long_long_name = %ix$' % i)

#ax.legend()
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
              fancybox=True, shadow=True, ncol=3)
fig.tight_layout(rect=[0, 0.1, 1, 0.95])
plt.show()

如何配置正确的图表和图例大小/位置? 我查看了Legend Guidethis post,但无法弄清楚如何让传奇变得更窄。

1 个答案:

答案 0 :(得分:1)

我建议您更改图例字体大小或图表大小。为此:

fig = plt.figure(figsize=(x_size, y_size))

尝试使用x_size = 8和y_size = 5.

或者

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=3, fontsize = size)

尝试使用size = 8。