图中增加显示window

Increase display window in figure

我正在使用 matplotlib 绘制一些直方图。正如你在图片中看到的那样,我的身材被标签覆盖了。 my image

我尝试以下操作:

plt.figure(num=None, figsize=(10, 8), 
          dpi=180, facecolor='w', edgecolor='k')

但实际上增加了整个window,仍然没有显示图形。我该如何解决?

修复它的一种方法是将大小调整为:

plt.legend(prop={'size':10},loc='upper right')

此外,我还指定了图例的位置。结果是:

这是一种方式。如果有人有其他建议,那将很有趣。

有时,减小图例的大小(尤其是标签的字体大小)不是一种选择。例如,两栏论文的一栏图表就是这种情况。

在你的具体情况下,我会:

  1. 去掉图例的边框;
  2. 将其位置更改为图的左上角;
  3. 控制y轴的极限;
  4. 更改标签的顺序,使较长的标签位于顶部;
  5. 为每个绘图提供独特的线条样式以进一步区分它们;
  6. 微调标签的字体大小及其在图例中的垂直间距。

下面是一个向您展示如何完成此操作的示例:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

plt.close('all')

fig = plt.figure()
ax = fig.add_axes([0.15, 0.15, 0.80, 0.80])

#---- generate some data ----

x = np.arange(-10, 10, 0.001)
y1 = norm.pdf(x, 0, 5.2)
y2 = norm.pdf(x, 0, 5)
y3 = norm.pdf(x, 0, 2)

#---- setup ticks ----

ax.tick_params(axis='both', direction='out', top='off', right='off')

#---- set axes labels ----

ax.set_ylabel('Log (frequency)', labelpad=15, fontsize=14)
ax.set_xlabel('Image values', labelpad=15, fontsize=14)

#---- plot data ----

h1, = ax.plot(x, y1, color='blue', lw=1.5)
h2, = ax.plot(x, y2, dashes=[2, 3], color='green', lw=2)
h3, = ax.plot(x, y3, dashes=[1.5, 5, 10, 5], color='red', lw=1.5)

#---- plot legend ----

lines = [h1, h2, h3]
labels = ['Transformed input image', 'Reference image', 'Input image']

ax.legend(lines, labels, bbox_to_anchor=(0, 1), loc='upper left', ncol=1, 
          fontsize=14, labelspacing=0.5, borderaxespad=0.5, frameon=False, 
          handletextpad=1, numpoints=1)

#---- set axis limits ----

ax.axis(ymax=0.22)

#---- show and save ----

plt.show(block=False)
fig.savefig('legend_overlap.png')

上面的代码导致:

更新(2015-07-29):

但是,如果您仍想在图例周围保留一个框架,您还可以调整图形的宽度和高度之间的比率,直到图例适合图表。这可以如下例所示完成:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

plt.close('all')

fwidth = 10
fheight = 5

fig = plt.figure(figsize=(fwidth, fheight))

left_margin  = 1.1 / fwidth
right_margin = 0.25 / fwidth
bottom_margin = 0.75 / fheight
top_margin = 0.25 / fheight

#---- generate an axe ----

h = 1 - (bottom_margin + top_margin)
w = 1 - (left_margin + right_margin)

ax = fig.add_axes([left_margin, bottom_margin, w, h])

#---- generate some data ----

x = np.arange(-10, 10, 0.001)
y1 = norm.pdf(x, 0, 5.2)
y2 = norm.pdf(x, 0, 5)
y3 = norm.pdf(x, 0, 2)

#---- setup ticks ----

ax.tick_params(axis='both', direction='out', top='off', right='off')

#---- set axes labels ----

ax.set_ylabel('Log (frequency)', labelpad=15, fontsize=14)
ax.set_xlabel('Image values', labelpad=15, fontsize=14)

#---- plot data ----

h1, = ax.plot(x, y1, color='blue', lw=1.5)
h2, = ax.plot(x, y2, dashes=[2, 3], color='green', lw=2)
h3, = ax.plot(x, y3, dashes=[1.5, 5, 10, 5], color='red', lw=1.5)

#---- plot legend ----

lines = [h1, h2, h3]
labels = ['Transformed input image', 'Reference image', 'Input image']

ax.legend(lines, labels, loc='upper right', ncol=1, fancybox=True,
          fontsize=14, labelspacing=0.5, handletextpad=1, numpoints=1)

#---- set axis limits ----

ax.axis(ymax=0.22)

#---- show and save ----

plt.show(block=False)
fig.savefig('legend_overlap_alternate.png')

这导致: