Alter
Alter

Reputation: 3464

Add caption to figure which has subplots

I want to add some text to a figure. I've read a few posts on the matter, but the answers don't seem to work when the figure has subplots.

Example: I would like the text to not overlap any images. enter image description here

Code:

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()

Upvotes: 1

Views: 4229

Answers (2)

Rikka
Rikka

Reputation: 1049

You can use plt.gcf().subplots_adjust(bottom=0.30) to adjust the bottom position for your subplots. I've tried your code with different arguments and found that 0.30 works for me.

Adding the line anywhere in your code before the show command should make it work.

Upvotes: 2

Ben
Ben

Reputation: 390

If you use the subplot2grid function, you can leave empty space at the bottom for the text. Make sure to call plt.tight_layout() so that your axes don't overlap.

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()

Upvotes: 1

Related Questions