【问题标题】:Matplotlib legend too wideMatplotlib 图例太宽
【发布时间】:2017-09-18 17:13:58
【问题描述】:

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

#!/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,但无法弄清楚如何使图例变窄。

【问题讨论】:

  • 为什么不直接指定ncol=2
  • 实际上效果很好。谢谢

标签: python matplotlib


【解决方案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。

【讨论】:

  • Font size = 8 太小了,但这是一种解决方案。
  • 我选择的大小只是一个建议,也许同时使用字体大小和图形大小会给你带来更好的结果。或者只是更大的尺寸。
猜你喜欢
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多