将多行文本添加到matplotlib数字的标题中

时间:2018-06-08 19:09:23

标签: python-3.x matplotlib

我想要一个具有以下功能的人物:   - 图名称,日期和样本数量为三个单独的行    数字标题。   - 图形大小根据随机数量的样本动态设置。

我已经包含了以下代码。它建立在我在堆栈溢出中找到的嵌套子图的代码的基础上。

输出问题:   - 标题位置变得动态。   - 数字尺寸太大,无法获得更高的样本数。

Example of incorrect output from current code

任何帮助表示赞赏。 感谢

代码:

# Importing packages
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import datetime as dt
import random
import string

# Choosing a random no. of alphabets as samples
samples=[]
for i in range(random.choice(range(10))+1):
    samples.append(random.choice(string.ascii_letters))

date=dt.datetime.today().strftime('%b-%d, %Y')

# Setting figure size ....(NEED HELP HERE in setting height of figure)
fig = plt.figure(figsize=(20,5*len(samples)))

# Setting the figure heading text ....(NEED HELP HERE with the text locations)
fig.suptitle('Title', fontsize=50, verticalalignment = "top", horizontalalignment = 'center')
fig.text(0.5,0.9, date, fontsize = 40)    
fig.text(0.5,0.85,"No. of samples: " + str(len(samples)), fontsize = 30)    

# Plotting
outer = gridspec.GridSpec((len(samples)), 2, wspace=0.2, hspace=0.2)

for i in range(len(samples)):
    inner = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=outer[i], wspace=0.0, hspace=0.0)

    for j in range(2):
        ax = plt.Subplot(fig, inner[j])
        t1 = ax.text(0.5,0.5, 'outer=%d, inner=%d' % (i,j))
        t1.set_ha('center')
        if j == 0:
            ax.set_title(samples[i])
        else:
            pass
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

fig.show()

0 个答案:

没有答案