亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何繪制 matplotlib.axes 列表?

如何繪制 matplotlib.axes 列表?

德瑪西亞99 2023-06-20 17:31:35
我正在使用一些返回 matplotlib.axes 對象的代碼。我使用的代碼是https://github.com/pblischak/HyDe/blob/master/phyde/visualize/viz.py上的 def_density() 函數 在一個循環中,我打開一個數據文件,解析每一行,輸入一些數據,創建一個 matplotlib.axes 對象(使用 seaborn.kdeplot() 方法),提取圖像,然后將圖像寫入文件。這很好用,但我只剩下每個文件行一張圖像。我真正想做的是收集所有圖像,將它們放在網格中并只創建一個圖像。我不會提前知道有多少圖像(當然我可以先計算行數,然后使用該信息重新遍歷文件)。這是我的代碼import phyde as hdimport osimport sysbootfile = 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)    我的問題是,如果我說要在 5 x 4 網格中設置 20 個地塊,我該怎么做?我已經使用我在此處找到的示例嘗試了許多不同的方法,但似乎沒有任何效果(plt.subplots() 是我玩過的東西)。任何建議都將非常受歡迎!
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

要將單獨的密度圖放置在網格中,您必須將網格中的一個軸傳遞給繪圖函數,但是,phyde.viz.density非常薄的一層seaborn.kdeplot,沒有為重用預先存在的axes.


所以你必須定義1你的薄層需要預先存在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)

然后用于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')

查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號