【问题标题】:matplotlib pyplot - subplots with sizes proportional to the datamatplotlib pyplot - 大小与数据成比例的子图
【发布时间】:2021-08-03 09:04:22
【问题描述】:

我想创建一个带有子图的图形,其中每个子图的大小与其包含的数据成正比。例如:

from matplotlib import pyplot as plt
from matplotlib_venn import venn2
data_dict = {'foo': (10,7,5), 'bar': (2,6,3), 'baz': (22,17,12)}
figure, axes = plt.subplots(1, 3)
i = 0
for x in ['foo','bar','baz']:
    venn2(subsets = data_dict[x], set_labels=('A', 'B'), ax=axes[i])
    i += 1

我想通过每个维恩图(A+B+相交)中的数字总和来缩放每个子图的大小。我该怎么做?

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    我是在 matplotlib 中绘制维恩图的新手,但我认为如果你在 gridspec_kw 中设置宽度比是可能的。我将字典数据中的数据进行汇总,确定组成比例,并将其指定为比例。

    from matplotlib import pyplot as plt
    from matplotlib_venn import venn2
    
    data_dict = {'foo': (10,7,5), 'bar': (2,6,3), 'baz': (22,17,12)}
    ratios = [sum(v) for v in data_dict.values()]
    norm = [n / sum(ratios) for n in ratios]
    
    figure, axes = plt.subplots(1, 3, gridspec_kw=dict(width_ratios=norm))
    i = 0
    for x in ['foo','bar','baz']:
        venn2(subsets = data_dict[x], set_labels=('A', 'B'), ax=axes[i])
        i += 1
    

    【讨论】:

    • 谢谢。如果子图网格是例如2x2 ?
    • 对于 2x2,您可以通过设置高度和宽度来做到这一点。例如,以下。 plt.subplots(2, 2, gridspec_kw=dict(width_ratios=[1,1], height_ratios=[2,1])
    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多