Matplotlib无法绘制所有子图

时间:2018-01-15 11:51:04

标签: python matplotlib

我想使用Python在一个图中动态绘制7或8个直方图(共享x轴)。但这个数字只显示了它的一部分。 enter image description here 虽然它有7个子图: enter image description here 这是我的代码:

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
import pickle
distribution = pickle.load(open("data.txt", "r"))
fig,axes = plt.subplots(nrows = len(distribution), sharex = True)
index=0
for tag in distribution_progress:
    axes[index].hist(distribution_[tag],bins=50, normed=1, facecolor='yellowgreen', alpha=0.75)
    axes[index].set_title(tag)
    index += 1
plt. subplots_adjust( top = 3, hspace = 0.4)
plt.show()

1 个答案:

答案 0 :(得分:1)

指定高度/宽度比较大的图形尺寸:

width, height = 6, 8
fig,axes = plt.subplots(nrows=N, sharex=True, figsize=(width, height))

增加hspace以为子图标题提供足够的空间:

plt. subplots_adjust(hspace = 1.0)

作为ImportanceOfBeingErnest pointed out,请勿使用top = 3,因为这会将子图的顶部放在Figure coordinate system的y = 3处,而可见图的顶部始终位于y = 1(在图坐标系中)。

import numpy as np
import matplotlib.pyplot as plt
N = 8
width, height = 6, 8
fig, axes = plt.subplots(nrows=N, sharex=True, figsize=(width, height))
index = 0
for tag in range(N):
    axes[index].hist(np.random.random(100), bins=50, normed=1,
                     facecolor='yellowgreen', alpha=0.75)
    axes[index].set_title(tag)
    index += 1
plt. subplots_adjust(hspace=1.0)
plt.show()

enter image description here