Subplot内的分页符?多页上的 Matplotlib 子图 [英] Pagebreak inside Subplot? Matplotlib subplot over mulitple pages

查看:49
本文介绍了Subplot内的分页符?多页上的 Matplotlib 子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个能够将多个图形绘制到一个 PDF 文件中的 Python 程序,但是子图的数量是可变的.我已经做到了每页一个情节.但是,由于我得到了大约100个情节的someteimes,使得很多滚动工作并没有真正清晰地显示出来.因此,我希望每页有 5X4 个 subpltot.我为此写了代码,整个代码很长,因为对于pyhton来说我是一个很新的人,对知道该怎么做的人来说这看起来很糟糕,但是绘图部分看起来像这样:

I want to create a python programm that is able to plot multiple graphs into one PDF file, however the number of subplots is variable. I did this already with one plot per page. However, since i got someteimes arround 100 plots that makes a lot of scrolling and is not really clearly shown. Therefore I would like to get like 5X4 subpltots per page. I wrote code for that alreaedy, the whole code is long and since im very new to pyhton it looks terrible to someone who knows what to do, however the ploting part looks like this:

rows = (len(tags))/5
fig = plt.figure()
count = 0    
for keyInTags in tags:
       count = count + 1
       ax = fig.add_subplot(int(rows), 5, count)
       ax.set_title("cell" + keyInTags)
       ax.plot(x, y_green, color='k')
       ax.plot(x, y_red, color='k')
       plt.subplots_adjust(hspace=0.5, wspace=0.3)
pdf.savefig(fig)

这个想法是,我得到了一个标有所有细胞"(用于生物学研究)的PDF.到目前为止,我编写的代码运行良好,但是如果我有超过 4 行的子图,我想做一个pageprake".在某些情况下,我在一页上有超过 21 行,这使得看不到任何内容.

The idea is that i get an PDF with all "cells" (its for biological research) ploted. The code I wrote is working fine so far, however if I got more than 4 rows of subplots I would like to do a "pageprake". In some cases i got over 21 rows on one page, that makes it impossible to see anything.

那么,是否有解决方案,例如,告诉 Python 在 4 行后进行分页?在 21 行 id 的情况下,喜欢有 6 页具有漂亮的可见图.或者它是通过做 5x4 绘图然后以某种方式迭代文件来完成的?如果有人可以提供一点帮助或提供提示,我将非常高兴.我坐在这里 4 个小时了,没有找到解决方案.

So, is there a solution to, for example, tell Python to do a page break after 4 rows? In the case with 21 rows id like to have 6 pages with nice visible plots. Or is it done by doing 5x4 plots and then iterating somehow over the file? I would be really happy if someone could help a little or give a hint. Im sitting here since 4 hours, not finding a solution.

推荐答案

A.循环页面

您可以找出您需要多少页 (npages) 并为每页创建一个新图.

A. Loop over pages

You could find out how many pages you need (npages) and create a new figure per page.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages


tags = ["".join(np.random.choice(list("ABCDEFG123"), size=5)) for _ in range(53)]

N = len(tags)  # number of subplots
nrows = 5      # number of rows per page
ncols = 4      # number of columns per page

# calculate number of pages needed
npages = N // (nrows*ncols)
if N % (nrows*ncols) > 0:
    npages += 1

pdf = PdfPages('out2.pdf')

for page in range(npages):
    fig = plt.figure(figsize=(8,11))
    for i in range(min(nrows*ncols, N-page*(nrows*ncols))):
        # Your plot here
        count = page*ncols*nrows+i
        ax = fig.add_subplot(nrows, ncols, i+1)
        ax.set_title(f"{count} - {tags[count]}")
        ax.plot(np.cumsum(np.random.randn(33)))
        # end of plotting

    fig.tight_layout()
    pdf.savefig(fig)

pdf.close()
plt.show()

B.循环数据

或者,您可以遍历标签本身并在需要时创建一个新图形:

B. Loop over data

Or alternatively you could loop over the tags themselves and create a new figure once it's needed:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages


tags = ["".join(np.random.choice(list("ABCDEFG123"), size=5)) for _ in range(53)]

nrows = 5      # number of rows per page
ncols = 4      # number of columns per page

pdf = PdfPages('out2.pdf')

for i, tag in enumerate(tags):
    j = i % (nrows*ncols)
    if j == 0:
        fig = plt.figure(figsize=(8,11))

    ax = fig.add_subplot(nrows, ncols,j+1)
    ax.set_title(f"{i} - {tags[i]}")
    ax.plot(np.cumsum(np.random.randn(33)))
    # end of plotting
    if j == (nrows*ncols)-1 or i == len(tags)-1:
       fig.tight_layout()
       pdf.savefig(fig)

pdf.close()
plt.show()

这篇关于Subplot内的分页符?多页上的 Matplotlib 子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆