【问题标题】:How to modify the font size in matplotlib-venn如何在 matplotlib-venn 中修改字体大小
【发布时间】:2015-04-03 04:11:41
【问题描述】:

我有以下维恩图:

from matplotlib import pyplot as plt
from matplotlib_venn import venn3, venn3_circles
set1 = set(['A', 'B', 'C', 'D'])
set2 = set(['B', 'C', 'D', 'E'])
set3 = set(['C', 'D',' E', 'F', 'G'])

venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))

看起来像这样:

如何控制绘图的字体大小? 我想增加它。

【问题讨论】:

    标签: python matplotlib venn-diagram matplotlib-venn


    【解决方案1】:

    如果outvenn3()返回的对象,则文本对象只是存储为out.set_labelsout.subset_labels,所以你可以这样做:

    from matplotlib import pyplot as plt
    from matplotlib_venn import venn3, venn3_circles
    set1 = set(['A', 'B', 'C', 'D'])
    set2 = set(['B', 'C', 'D', 'E'])
    set3 = set(['C', 'D',' E', 'F', 'G'])
    
    out = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))
    for text in out.set_labels:
        text.set_fontsize(14)
    for text in out.subset_labels:
        text.set_fontsize(16)
    

    【讨论】:

    • out.set_labels 工作正常,但尝试选择 out.subset_labels 时出现以下错误:AttributeError: 'NoneType' object has no attribute 'set_fontsize'
    【解决方案2】:

    在我的例子中,out.subset_labels 的一些值是 NoneType。为了省略这个问题,我做了:

    from matplotlib import pyplot as plt
    from matplotlib_venn import venn3, venn3_circles
    set1 = set(['A', 'B', 'C', 'D'])
    set2 = set(['B', 'C', 'D', 'E'])
    set3 = set(['C', 'D',' E', 'F', 'G'])
    
    out = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))
    for text in out.set_labels:
        text.set_fontsize(15)
    for x in range(len(out.subset_labels)):
        if out.subset_labels[x] is not None:
            out.subset_labels[x].set_fontsize(15)
    

    【讨论】:

      【解决方案3】:

      我只是在 Matplotlib 中更改了字体大小(也可以使用 rcParams 更改文本颜色)。

      from matplotlib_venn import venn3
      from matplotlib import pyplot as plt
      plt.figure(figsize = (6, 5))
      font1 = {'family':'serif','color':'black','size':16} # use for title
      font2 = {'family': 'Comic Sans MS', 'size': 8.5} # use for labels
      plt.rc('font', **font2) # sets the default font 
      plt.rcParams['text.color'] = 'darkred' # changes default text colour
      venn3(subsets=({'A', 'B', 'C', 'D', 'X', 'Y', 'Z'}, 
                     {'A', 'B', 'E', 'F', 'P'}, {'B', 'C', 'E', 'G'}), 
            set_labels=('Set1', 'Set2', 'Set3'),
            set_colors=("coral", "skyblue", "lightgreen"), alpha=0.9)
      
      plt.title("Changing Font Size in Matplotlib_venn Diagrams", fontdict=font1)
      plt.show()
      
      

      【讨论】:

        猜你喜欢
        • 2014-08-12
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 2011-04-23
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        相关资源
        最近更新 更多