首页 文章

你如何改变用matplotlib绘制的数字大小?

提问于
浏览
1254

你如何改变用matplotlib绘制的图形的大小?

13 回答

  • 9

    尝试注释掉 fig = ...

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 50
    x = np.random.rand(N)
    y = np.random.rand(N)
    area = np.pi * (15 * np.random.rand(N))**2
    
    fig = plt.figure(figsize=(18, 18))
    plt.scatter(x, y, s=area, alpha=0.5)
    plt.show()
    
  • 69

    如果您正在寻找一种方法来改变Pandas中的数字大小,您可以这样做:

    df['some_column'].plot(figsize=(10, 5))
    

    其中 df 是Pandas数据帧 . 如果要更改默认设置,可以执行以下操作:

    import matplotlib
    
    matplotlib.rc('figure', figsize=(10, 5))
    
  • 8

    请尝试以下简单代码:

    from matplotlib import pyplot as plt
    plt.figure(figsize=(1,1))
    x = [1,2,3]
    plt.plot(x, x)
    plt.show()
    

    您需要在绘图前设置图形大小 .

  • 23

    你可以简单地使用(来自matplotlib.figure.Figure):

    fig.set_size_inches(width,height)
    

    从Matplotlib 2.0.0开始,对画布的更改将立即显示为 forward 关键字defaults to True .

    如果您只想change the width or height而不是两者,您可以使用

    fig.set_figwidth(val)fig.set_figheight(val)

    这些也会立即更新您的画布,但仅限于Matplotlib 2.2.0和更新版本 .

    适用于旧版本

    您需要明确指定 forward=True 才能在早于上面指定版本的版本中实时更新画布 . 请注意, set_figwidthset_figheight 函数不支持早于Matplotlib 1.5.0的版本中的 forward 参数 .

  • 172

    由于Matplotlib isn't able本身使用公制系统,如果要以合理的长度单位(例如厘米)指定图形的大小,则可以执行以下操作(代码来自gns-ank):

    def cm2inch(*tupl):
        inch = 2.54
        if isinstance(tupl[0], tuple):
            return tuple(i/inch for i in tupl[0])
        else:
            return tuple(i/inch for i in tupl)
    

    然后你可以使用:

    plt.figure(figsize=cm2inch(21, 29.7))
    
  • 45

    如果您已经创建了图形,则可以快速执行此操作:

    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(18.5, 10.5)
    fig.savefig('test2png.png', dpi=100)
    

    要将大小更改传播到现有gui窗口,请添加 forward=True

    fig.set_size_inches(18.5, 10.5, forward=True)
    
  • 153

    figure告诉你呼叫签名:

    from matplotlib.pyplot import figure
    figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
    

    figure(figsize=(1,1)) 将创建一个逐英寸的图像,除非你也提供不同的dpi参数,否则它将是80×80像素 .

  • 13

    弃用说明:根据官方Matplotlib指南,不再推荐使用pylab模块 . 请考虑使用matplotlib.pyplot模块,如其他答案所述 .

    以下似乎有效:

    from pylab import rcParams
    rcParams['figure.figsize'] = 5, 10
    

    这使得图形的宽度为5英寸,高度为10 inches .

    然后,Figure类将其用作其中一个参数的默认值 .

  • 749

    即使在绘制图形之后,这也会立即调整大小(至少使用Qt4Agg / TkAgg - 但不是MacOSX - 使用matplotlib 1.4.0):

    matplotlib.pyplot.get_current_fig_manager().resize(width_px, height_px)
    
  • 277

    使用plt.rcParams

    如果您想在不使用图形环境的情况下更改大小,还可以使用此解决方法 . 因此,如果你使用plt.plot(),你可以设置一个宽度和高度的元组 .

    import matplotlib.pyplot as plt
    plt.rcParams["figure.figsize"] = (20,3)
    

    当你内联绘图时(例如使用IPython Notebook),这非常有用 . 正如@asamaier注意到你最好不要将这个语句放在imports语句的同一个单元格中 .

    转换为cm

    figsize 元组接受英寸,所以如果你想把它设置为厘米,你必须将它们除以2.54,看看this question .

  • 22

    这适合我:

    from matplotlib import pyplot as plt
    F = gcf()
    Size = F.get_size_inches()
    F.set_size_inches(Size[0]*2, Size[1]*2, forward=True)#Set forward to True to resize window along with plot in figure.
    plt.show() #or plt.imshow(z_array) if using an animation, where z_array is a matrix or numpy array
    

    这也可能会有所帮助:http://matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html

  • 568

    要增加图形的大小N次,您需要在pl.show()之前插入它:

    N = 2
    params = pl.gcf()
    plSize = params.get_size_inches()
    params.set_size_inches( (plSize[0]*N, plSize[1]*N) )
    

    它也适用于ipython笔记本 .

  • 7

    Google中 'matplotlib figure size' 的第一个链接是AdjustingImageSizeGoogle cache of the page) .

    这是上一页的测试脚本 . 它会创建相同图像的不同大小的 test[1-3].png 文件:

    #!/usr/bin/env python
    """
    This is a small demo file that helps teach how to adjust figure sizes
    for matplotlib
    
    """
    
    import matplotlib
    print "using MPL version:", matplotlib.__version__
    matplotlib.use("WXAgg") # do this before pylab so you don'tget the default back end.
    
    import pylab
    import numpy as np
    
    # Generate and plot some simple data:
    x = np.arange(0, 2*np.pi, 0.1)
    y = np.sin(x)
    
    pylab.plot(x,y)
    F = pylab.gcf()
    
    # Now check everything with the defaults:
    DPI = F.get_dpi()
    print "DPI:", DPI
    DefaultSize = F.get_size_inches()
    print "Default size in Inches", DefaultSize
    print "Which should result in a %i x %i Image"%(DPI*DefaultSize[0], DPI*DefaultSize[1])
    # the default is 100dpi for savefig:
    F.savefig("test1.png")
    # this gives me a 797 x 566 pixel image, which is about 100 DPI
    
    # Now make the image twice as big, while keeping the fonts and all the
    # same size
    F.set_size_inches( (DefaultSize[0]*2, DefaultSize[1]*2) )
    Size = F.get_size_inches()
    print "Size in Inches", Size
    F.savefig("test2.png")
    # this results in a 1595x1132 image
    
    # Now make the image twice as big, making all the fonts and lines
    # bigger too.
    
    F.set_size_inches( DefaultSize )# resetthe size
    Size = F.get_size_inches()
    print "Size in Inches", Size
    F.savefig("test3.png", dpi = (200)) # change the dpi
    # this also results in a 1595x1132 image, but the fonts are larger.
    

    输出:

    using MPL version: 0.98.1
    DPI: 80
    Default size in Inches [ 8.  6.]
    Which should result in a 640 x 480 Image
    Size in Inches [ 16.  12.]
    Size in Inches [ 16.  12.]
    

    两个说明:

    • 模块注释和实际输出不同 .

    • This answer允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异 .

相关问题