简体   繁体   English

如何绘制 matplotlib.axes 列表?

[英]How do I plot a list of matplotlib.axes?

I'm using some code that returns a matplotlib.axes object.我正在使用一些返回 matplotlib.axes 对象的代码。 The code I'm using is the def_density() function at https://github.com/pblischak/HyDe/blob/master/phyde/visualize/viz.py In a loop, I open a data file, parse each line, feed in some data, create an matplotlib.axes object (using the seaborn.kdeplot() method), extract the image and then write the image to file.我使用的代码是https://github.com/pblischak/HyDe/blob/master/phyde/visualize/viz.py 上的 def_density() 函数在一个循环中,我打开一个数据文件,解析每一行,输入一些数据,创建一个 matplotlib.axes 对象(使用 seaborn.kdeplot() 方法),提取图像,然后将图像写入文件。 That works fine, but I'm left with one image per file line.这工作正常,但我每个文件行只剩下一个图像。 What I really want to do is to gather all of the images, lay them out in a grid and create only one image.我真正想做的是收集所有图像,将它们放在网格中,然后只创建一个图像。 I won't know how many images I'll have ahead of time (although of course I could count the number of lines first and then re-iterate over the file with that information).我不知道我会有多少图像提前(尽管我当然可以先计算行数,然后使用该信息重新遍历文件)。

Here's my code这是我的代码

import phyde as hd
import os
import sys

bootfile = sys.argv[1]
triplesfile = sys.argv[2]

boot = hd.Bootstrap(bootfile)
triples=open(triplesfile, "r")

next(triples)
for line in triples:
    triple = line.split()
    p1=triple[0]
    p2=triple[1]
    p3=triple[2]
    title="Bootstrap Dist. of Gamma for "+p1+", "+p2+", and "+p3
    image= hd.viz.density(boot, 'Gamma', p1, p2, p3, title=title, xlab="Gamma", ylab="Density")
    fig = image.get_figure()
    
    figname="density_"+p1+"_"+p2+"_"+p3+".png"
    fig.savefig(figname)
    

My question is, if I have say 20 plots that I want to set out in a 5 x 4 grid, how do I do this?我的问题是,如果我想在 5 x 4 网格中设置 20 个图,我该怎么做? I've tried a number of different methods using examples I found on here, but nothing seems to work (plt.subplots() was something I played around with).我已经使用我在这里找到的示例尝试了许多不同的方法,但似乎没有任何效果(plt.subplots() 是我玩过的东西)。 Any suggestions would be extremely welcome!任何建议将非常受欢迎!

To place the individual density plots in a grid, you have to pass to the plotting function one of the axes in the grid but phyde.viz.density , a very thin layer over seaborn.kdeplot , doesn't make a provision for reusing a pre existing axes .为了将各个密度图在网格中,你必须通过在格但轴的绘图功能一个phyde.viz.density ,在很薄的一层seaborn.kdeplot ,不作拨备重用预先存在的axes

So you have to define¹ your thin layer that requires a pre existing axes所以你必须定义¹需要预先存在的axes薄层

def density2(ax, boot_obj, attr, p1, hyb, p2, title="", xlab="", ylab="", shade=True, color='b'):
    from numpy import array
    from seaborn import kdeplot, set_style
    set_style("white")
    kdeplot(array(boot_obj(attr, p1, hyb, p2)), shade=shade, color=color, ax=ax)
    #                                                                     #####
    ax.set(ylabel=ylab, xlabel=xlab, title=title)

and then use plt.subplots to arrange the individual plots in a grid (you mentioned 5×4 in your question)然后使用plt.subplots在网格中排列各个图(您在问题中提到了 5×4)

...
fig, axes = plt.subplots(5, 4, constrained_layout=True)
for row in axes:
    for ax in row:
        line = next(triples)
        ...
        density2(ax, boot, 'Gamma', p1, p2, p3, title=title, xlab="Gamma", ylab="Density")
fig.savefig('You have to decide a title for the collection of plots')

1: the function I've defined is (of course) adapted from https://hybridization-detection.readthedocs.io/_modules/phyde/visualize/viz.html 1:我定义的函数(当然)改编自https://hybridization-detection.readthedocs.io/_modules/phyde/visualize/viz.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM