matplotlib로 그린 그림의 크기를 어떻게 변경하나요?
그림은 통화 서명을 알려줍니다:
from matplotlib.pyplot import figure
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
figure(figsize=(1,1))`는 인치 단위 이미지를 생성하며, 다른 dpi 인수를 지정하지 않는 한 80×80픽셀이 됩니다.
또한 경우에 대비하여 이 해결 방법을 사용하지 않고 그림 크기를 변경할 수 있습니다. 그래서 '플t.프로트 ()' 에서 사용하는 경우, 예를 들어, 튜플 폭과 높이를 함께 설정할 수 있습니다.
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,3)
이것은 매우 유용한 플롯할 때는 인라인 (예를 들어 있는 이피통 노트북). 이 같은 발언은 이 같은 셀 수 없는 것을 사용하는 것이 더 좋을 수도 @asamaier 서피스로서 동일팔레트에 임포트합니다 명령문입니다.
이 때문에 튜플 사용할 수 있는 '인치' 프리시즈 스케쳐내 나눠 가질 수 있도록 함으로써 포착하십시오 2.54 cm 이 설정할 수 있습니다 (이 질문] 2.
'matplotlib 그림 크기'에 대한 Google의 첫 번째 링크는 AdjustingImageSize (페이지의 Google 캐시)입니다.
다음은 위 페이지의 테스트 스크립트입니다. 이 스크립트는 동일한 이미지의 크기가 다른 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.]
두 가지 참고 사항:
모듈 주석과 실제 출력은 다릅니다.
이 답변를 사용하면 세 이미지를 모두 하나의 이미지 파일에 결합하여 크기 차이를 쉽게 확인할 수 있습니다.
df['some_column'].plot(figsize=(10, 5))
여기서 'df' 은 다테프라임 판다. 기존의 그림 또는 axes 사용할 수 있고,
fig, ax = plt.subplots(figsize=(10,5))
df['some_column'].plot(ax=ax)
할 수 있는 기본 설정을 변경할 경우, 다음과 같다.
import matplotlib
matplotlib.rc('figure', figsize=(10, 5))
간단히 사용할 수 있습니다 () 의 매플로리발피게레스피구레).
fig.set_size_inches(width,height)
2.0.0 매플로리브 vmware. 변경 사항을 즉시 볼 수 있기 때문에, 앞으로 '키워드' 캔바스를 [설정값으로 'True' 로] [2].
그냥 스케쳐내 [변경하십시오 너버 net/ 또는 높이] [3] 모두 대신 사용할 수 있습니다.
'fig.set_figwidth (발)' 또는 'fig.set_figheight (발)'
하지만 이 또한 즉시 업데이트하십시오 캔바스를 에서만 매플로리브 2.2.0 및 새로운.
'앞으로' 명시적으로 지정해야 합니다 = True 실시간 업데이트 버전 이전의 너회의 캔바스를 위해 지정된 지정하십시오. 명이다. 참고로, '' 와 '는' 앞으로 '안' 기능을 set_figwidth set_figheight 매개변수입니다 버전 이전의 매플로리브 1.5.0.
[2]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html = set% 20size% 20inches # matplotlib.figure.figure.set_size_inches 강조표시할? [3]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html = set% 20size% 20inches # matplotlib.figure.figure.set_figheight 강조표시할?
'선' 주석 달기 아웃해야 시도하시겠습니까 그림 =.
%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()
이 만족스럽게 봐요.
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
이후 매플로리브 [isn& 수, t # 39] (https://github.com/matplotlib/matplotlib/issues/1369) 를 사용하여 시스템 메트릭은 기본적으로 스케쳐내 크기를 지정할 수 센티미터, 길이 단위를 너회의 그림과 같은 합리적인 다음과 같이 할 수 있습니다 (code 에서 [gns 행] (https://stackoverflow.com/a/22787457/395857):
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))