使用Seaborn调整子图的大小 [英] Resize subplots using seaborn

查看:59
本文介绍了使用Seaborn调整子图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始使用 matplotlib 和 seaborn 来绘制我的图表.这是我目前写的代码

I have just recently started to use matplotlib and seaborn to plot my graphs. This is the code that I wrote so far

count = 1
l=[13,0,47,29,10]
plt.figure(figsize=(30,40))

for ww in l:
    temp_dict = defaultdict(list)
    entropies = list()
    for k,v in df.ix[ww].iteritems():
        e = 0
        for i in v:
            temp_dict[k].append(float(i))
            if not float(i) == 0:
                e += -1.0*float(i)*log10(float(i))
        entropies.append(e)

    y = entropies
    x=(range(len(entropies)))
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

    plt.subplot(len(lista_autori),2,count)
    tdf = pd.DataFrame.from_dict(temp_dict, orient='columns')
    a = tdf.dropna()
    sns.set(font_scale=2)
    #sns.factorplot(size=2, aspect=1)
    sns.heatmap(a,linewidths=.5,cmap="RdBu_r")
    plt.xlabel(ur'year')
    plt.ylabel(ur'$PACS$')
    count +=1

    plt.subplot(len(lista_autori),2,count)
    plt.plot(x,y)
    x1 = (range(28))
    y1 =  [slope*i + intercept for i in x1]
    plt.plot(x1,y1)
    count +=1


plt.tight_layout();

结果如下:

我想调整每一行的大小,将行的 2/3 分配给左侧图片,剩余的分配给右侧图片.我试图查看 here 给出的答案,但是当我必须混合 ax 时我发现了一些困难和seaborn的热图.有任何帮助或其他解决方案吗?

I would like to resize each row, assigning 2/3 of the row to the left hand side picture, the remaining to the right one. I tried to look at the answer given here but I found some difficulties when I have to mix ax and seaborn's heatmap. Any help or other solutions?

推荐答案

链接的问题的答案 直接告诉你如何使用GridSpec实现列宽不等的网格.我在这里看不出太大的区别,但是以下内容可能对您更容易理解,因为它使用了seaborn热图,多了一行并且没有"ax".

The answer to the linked question directly tells you how to achieve a grid with unequal column width using GridSpec. I do not see much of a difference here, but maybe the following is more understandable for you, because it uses seaborn heatmaps, more than one row and no "ax".

import numpy as np
import matplotlib.pyplot as plt 
from matplotlib import gridspec
import seaborn as sns

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
X = np.random.rand(3,4)

fig = plt.figure(figsize=(8, 6)) 
gs = gridspec.GridSpec(5, 2, width_ratios=[2, 1]) 

for i in range(5):
    plt.subplot(gs[i*2+0])
    sns.heatmap(X)
    plt.subplot(gs[i*2+1])
    plt.plot(x,y)

plt.show()

这篇关于使用Seaborn调整子图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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