为具有子图的图添加标题

时间:2016-02-22 04:19:07

标签: python matplotlib

我想在图中添加一些文字。我已经阅读了一些关于这个问题的帖子,但是当这个数字有子图时,答案似乎不起作用。

示例: 我希望文本不与任何图像重叠。 enter image description here

代码:

import numpy
import matplotlib.pyplot as plt

bincount = 100
mean, stdev, population = 0, 1, 10000
separation = 3*stdev

norm1 = numpy.random.normal(mean, stdev, population)
norm2 = numpy.random.normal(mean + separation, stdev, population)
comb = numpy.concatenate([norm1, norm2]) #concatenate nd_arrays

plt.figure("Bayesian")
plt.subplot(313)
plt.title("Combined")
hist3 = plt.hist(comb, bincount) #population, bins, patch = plt.hist()

plt.subplot(221)
plt.title("Class 1")
hist1 = plt.hist(norm1, bins=hist3[1])

plt.subplot(222)
plt.title("Class 2")
hist2 = plt.hist(norm2, bins=hist3[1])

index = int(len(hist3[1])/2)#arbitrarily choosing the middle bin
bin = hist3[1][index]
binsize = (max(hist3[1])-min(hist3[1]))/len(hist3[1])
probIndex = 1/len(hist3[1])
probClass = len(norm1)/len(comb)
condProbClass = hist1[0][index]/sum(hist1[0])
condProbIndex = probClass*condProbClass/probIndex

t = "An item is found at index {idx}, which corresponds to {binStart}-{binEnd}. What is the probabilty it belongs to class 1?\n\n"\
    "Given this, the probability of any item being Class 1 is {pC1}\n"\
    "The probabilty of any item being found at index {idx} is {pInd}\n"\
    "The conditional probability of the item falling in this index given it belongs to class 1 is {cpC1}\n"\
    "Using Bayes-Theorem, we can now find the probability of an item found at this index belongs to class 1 is {cpI}"\
    .format(
    idx=index,
    binStart=bin-binsize/2,
    binEnd=bin+binsize/2,
    pC1=probClass,
    pInd=probIndex,
    cpC1=condProbClass,
    cpI=condProbIndex
)

plt.figtext(0, 0, t, wrap=True)
plt.show()

2 个答案:

答案 0 :(得分:2)

您可以使用plt.gcf().subplots_adjust(bottom=0.30)调整子图的底部位置。我尝试了不同参数的代码,发现0.30对我有用。

show命令之前在代码中的任何位置添加行应该可以使它工作。

答案 1 :(得分:1)

如果使用subplot2grid函数,则可以在文本底部留下空白区域。请务必致电plt.tight_layout(),以便您的轴不重叠。

import numpy
import matplotlib.pyplot as plt

bincount = 100
mean, stdev, population = 0, 1, 10000
separation = 3*stdev

norm1 = numpy.random.normal(mean, stdev, population)
norm2 = numpy.random.normal(mean + separation, stdev, population)
comb = numpy.concatenate([norm1, norm2]) #concatenate nd_arrays

fig = plt.figure("Bayesian")

plt.subplot2grid((4, 4), (2, 0), colspan=4, rowspan=1)
plt.title("Combined")
hist3 = plt.hist(comb, bincount) #population, bins, patch = plt.hist()

plt.subplot2grid((4, 4), (0, 0), colspan=2, rowspan=2)
plt.title("Class 1")
hist1 = plt.hist(norm1, bins=hist3[1])

plt.subplot2grid((4, 4), (0, 2), colspan=2, rowspan=2)
plt.title("Class 2")
hist2 = plt.hist(norm2, bins=hist3[1])

index = int(len(hist3[1])/2)#arbitrarily choosing the middle bin
bin = hist3[1][index]
binsize = (max(hist3[1])-min(hist3[1]))/len(hist3[1])
probIndex = 1/len(hist3[1])
probClass = len(norm1)/len(comb)
condProbClass = hist1[0][index]/sum(hist1[0])
condProbIndex = probClass*condProbClass/probIndex

t = "An item is found at index {idx}, which corresponds to {binStart}-{binEnd}. What is the probabilty it belongs to class 1?\n\n"\
    "Given this, the probability of any item being Class 1 is {pC1}\n"\
    "The probabilty of any item being found at index {idx} is {pInd}\n"\
    "The conditional probability of the item falling in this index given it belongs to class 1 is {cpC1}\n"\
    "Using Bayes-Theorem, we can now find the probability of an item found at this index belongs to class 1 is {cpI}"\
    .format(
    idx=index,
    binStart=bin-binsize/2,
    binEnd=bin+binsize/2,
    pC1=probClass,
    pInd=probIndex,
    cpC1=condProbClass,
    cpI=condProbIndex
)

plt.tight_layout()
plt.figtext(0, 0, t, wrap=True)
plt.show()