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

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

对该问题的评论 (10)

告诉你调用签名。

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

figure(figsize=(1,1))将创建一个英寸的图像,也就是80×80像素,除非你还给出一个不同的dpi参数。

评论(6)

如果你'已经创建了人物,你可以快速地做到这一点。

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)
评论(2)

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

下面的方法似乎可以工作。

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

这使得该图的宽度为5英寸,高度为10英寸。

然后,Figure类将此作为其一个参数的默认值。

评论(6)

使用 plt.rcParams

如果你想在不使用图形环境的情况下改变尺寸,也有这种变通方法。 例如,如果你使用[plt.plot()][1],你可以设置一个包含宽度和高度的元组。

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

当你进行内联绘图时,这个功能非常有用(例如,使用IPython Notebook)。 IPython Notebook)。) 正如 @asamaier 所注意到的那样,最好不要把这个语句放在导入语句的同一个单元格中。

转换为cm

figsize元组接受的是英寸,所以如果你想以厘米为单位设置,就必须将它们除以2.54,请看[本题][2]。

[1]: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html [2]: https://stackoverflow.com/questions/14708695/specify-figure-size-in-centimeter-in-matplotlib

评论(8)

请试着写一个简单的代码,如下。

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

绘制之前需要设置好数字的大小。

评论(5)

在谷歌中,"'matplotlib数字大小' "的第一个链接是AdjustingImageSize([谷歌缓存页面](https://webcache.googleusercontent.com/search?q=cache:5oqjjm8c8UMJ:https://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/AdjustingImageSize.html+&cd=2&hl=en&ct=clnk&gl=fr))。

这里是上述页面的测试脚本。它创建了同一图片的不同尺寸的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.]

两个音符。

1.模块的注释和实际输出不同。

2.这个答案可以很容易地将所有三张图片合并到一个图片文件中,以查看尺寸的差异。

评论(1)

如果您正在寻找在Pandas中改变人物尺寸的方法,您可以这样做,例如:

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

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

其中df是一个Pandas数据框架。 或者,使用现有的图形或轴

fig, ax = plt.subplots(figsize=(10,5))
df['some_column'].plot(ax=ax)

如果您想更改默认设置,可以执行以下操作。

import matplotlib

matplotlib.rc('figure', figsize=(10, 5))
评论(0)

您可以简单地使用(来自[matplotlib.figure.Figure][1])。

fig.set_size_inches(width,height)

从Matplotlib 2.0.0开始,对画布的更改将立即可见,因为forward关键字[默认为True][2]。

如果您只想[改变宽度高度][3],而不是同时改变宽度和高度,您可以使用

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

这些也会立即更新您的画布,但只在Matplotlib 2.2.0和更新版本中。

对于旧版本

您需要明确指定forward=True,以便在比上面指定的版本更老的版本中实时更新您的画布。 请注意,在Matplotlib 1.5.0以上的版本中,"set_figwidth "和 "set_figheight "函数不支持 "forward "参数。

[1]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html [2]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=set%20size%20inches#matplotlib.figure.Figure.set_size_inches [3]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=set%20size%20inches#matplotlib.figure.Figure.set_figheight [4]: https://github.com/matplotlib/matplotlib/issues/9669

评论(0)

试着把 "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()
评论(0)
import matplotlib.pyplot as plt
plt.figure(figsize=(20,10))
plt.plot(x,y) ## This is your plot
plt.show()

您也可以使用。

fig, ax = plt.subplots(figsize=(20, 10))
评论(0)

如果要将数字的大小增加N倍,你需要在pl.show()之前插入这个。

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

它也能很好地与ipython笔记本配合使用。

评论(0)

这对我来说很有效。

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

评论(0)

由于 Matplotlib is&39;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))
评论(0)

这个功能可以在图形绘制完成后立即调整图形的大小(至少在使用Qt4Agg/TkAgg时是这样,但不是MacOSX,而是使用matplotlib 1.4.0)。

matplotlib.pyplot.get_current_fig_manager().resize(width_px, height_px)
评论(0)

另一个选择,使用matplotlib中的rc()函数(单位是英寸)

import matplotlib
matplotlib.rc('figure', figsize=[10,5])
评论(0)