Skip to main content
Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare), etc (the other is removal of trailing whitespace (secondary. It doesn't affect the rendered page.)).
Source Link
Peter Mortensen
  • 31.1k
  • 22
  • 111
  • 134
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.]
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.]
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.]
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.]
Rollback to Revision 5
Source Link

As per SciPy Cookbook: Matplotlib: adjusting image size, updated to useThe first link in Google for matplotlib.pyplot'matplotlib figure size' instead ofis pylabAdjustingImageSize (Google cache of the page).

Here's a test script from the above page. It creates test[1-3].png files of different sizes of the same image, and uses the following methods to adjust the size of the plot figures:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

print("using MPL version:", mpl.__version__)

# Generate and plot some simple data:
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

# plot the sample data
plt.plot(x, y)

# get the current figure
F = plt.gcf()

# Check everything with the defaults:
DPI = F.get_dpi()  # dpi of the current figure, not the saved image
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")

# Makes the image twice as big
F.set_size_inches((DefaultSize[0]*2, DefaultSize[1]*2))
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test2.png")

# Makes the image twice as big, making all the fonts and lines larger too
F.set_size_inches(DefaultSize)# reset the size
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test3.png", dpi=(200)) # change the dpi
#!/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: 3.6.2
DPI: 72.0
Default size in Inches [6. 4.]
Which should result in a 432 x 288 Image
Size in Inches [12.  8.]
Size in Inches [6. 4.]
  • Right click and open images in a new tab to see the full size

  • (432 x 288)

enter image description here

  • This image is increased 2x over the original (864 x 576)

enter image description here

  • Changing the dpi increased the image size (1200 x 800), the font size, and the line width.
    • (200 dpi / 72 dpi) * (432 x 288) = (1200 x 800)

enter image description here

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.]

NoteTwo notes:

  1. This answer combines all three images in one image file to see the difference in sizes.

    The module comments and the actual output differ.

  2. This answer allows easily to combine all three images in one image file to see the difference in sizes.

As per SciPy Cookbook: Matplotlib: adjusting image size, updated to use matplotlib.pyplot instead of pylab. It creates test[1-3].png files of different sizes of the same image, and uses the following methods to adjust the size of the plot figures:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

print("using MPL version:", mpl.__version__)

# Generate and plot some simple data:
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

# plot the sample data
plt.plot(x, y)

# get the current figure
F = plt.gcf()

# Check everything with the defaults:
DPI = F.get_dpi()  # dpi of the current figure, not the saved image
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")

# Makes the image twice as big
F.set_size_inches((DefaultSize[0]*2, DefaultSize[1]*2))
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test2.png")

# Makes the image twice as big, making all the fonts and lines larger too
F.set_size_inches(DefaultSize)# reset the size
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test3.png", dpi=(200)) # change the dpi
using MPL version: 3.6.2
DPI: 72.0
Default size in Inches [6. 4.]
Which should result in a 432 x 288 Image
Size in Inches [12.  8.]
Size in Inches [6. 4.]
  • Right click and open images in a new tab to see the full size

  • (432 x 288)

enter image description here

  • This image is increased 2x over the original (864 x 576)

enter image description here

  • Changing the dpi increased the image size (1200 x 800), the font size, and the line width.
    • (200 dpi / 72 dpi) * (432 x 288) = (1200 x 800)

enter image description here

Note:

  1. This answer combines all three images in one image file to see the difference in sizes.

The first link in Google for 'matplotlib figure size' is AdjustingImageSize (Google cache of the page).

Here's a test script from the above page. It creates test[1-3].png files of different sizes of the same image:

#!/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.]

Two notes:

  1. The module comments and the actual output differ.

  2. This answer allows easily to combine all three images in one image file to see the difference in sizes.

added 54 characters in body
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215

As per SciPy Cookbook: Matplotlib: adjusting image size, updated to use matplotlib.pyplot instead of pylab. It creates test[1-3].png files of different sizes of the same image, and uses the following methods to adjust the size of the plot figures:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

print("using MPL version:", mpl.__version__)

# Generate and plot some simple data:
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

# plot the sample data
plt.plot(x, y)

# get the current figure
F = plt.gcf()

# Check everything with the defaults:
DPI = F.get_dpi()  # dpi of the current figure, not the saved image
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")

# Makes the image twice as big
F.set_size_inches((DefaultSize[0]*2, DefaultSize[1]*2))
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test2.png")

# Makes the image twice as big, making all the fonts and lines larger too
F.set_size_inches(DefaultSize)# reset the size
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test3.png", dpi=(200)) # change the dpi

Output:

using MPL version: 3.6.2
DPI: 72.0
Default size in Inches [6. 4.]
Which should result in a 432 x 288 Image
Size in Inches [12.  8.]
Size in Inches [6. 4.]
  • Right click and open images in a new tab to see the full size

  • (432 x 288)

enter image description here

  • This image is increased 2x over the original (864 x 576)

enter image description here

  • Changing the dpi increased the image size (1200 x 800), the font size, and the line width.
    • (200 dpi / 72 dpi) * (432 x 288) = (1200 x 800)

enter image description here

Note:

  1. This answer combines all three images in one image file to see the difference in sizes.

As per SciPy Cookbook: Matplotlib: adjusting image size, updated to use matplotlib.pyplot instead of pylab. It creates test[1-3].png files of different sizes of the same image, and uses the following methods to adjust the size of the plot figures:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

print("using MPL version:", mpl.__version__)

# Generate and plot some simple data:
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

# plot the sample data
plt.plot(x, y)

# get the current figure
F = plt.gcf()

# Check everything with the defaults:
DPI = F.get_dpi()  # dpi of the current figure, not the saved image
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")

# Makes the image twice as big
F.set_size_inches((DefaultSize[0]*2, DefaultSize[1]*2))
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test2.png")

# Makes the image twice as big, making all the fonts and lines larger too
F.set_size_inches(DefaultSize)# reset the size
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test3.png", dpi=(200)) # change the dpi

Output:

using MPL version: 3.6.2
DPI: 72.0
Default size in Inches [6. 4.]
Which should result in a 432 x 288 Image
Size in Inches [12.  8.]
Size in Inches [6. 4.]
  • Right click and open images in a new tab to see the full size

  • (432 x 288)

enter image description here

  • This image is increased 2x over the original (864 x 576)

enter image description here

  • Changing the dpi increased the image size (1200 x 800), the font size, and the line width

enter image description here

Note:

  1. This answer combines all three images in one image file to see the difference in sizes.

As per SciPy Cookbook: Matplotlib: adjusting image size, updated to use matplotlib.pyplot instead of pylab. It creates test[1-3].png files of different sizes of the same image, and uses the following methods to adjust the size of the plot figures:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

print("using MPL version:", mpl.__version__)

# Generate and plot some simple data:
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

# plot the sample data
plt.plot(x, y)

# get the current figure
F = plt.gcf()

# Check everything with the defaults:
DPI = F.get_dpi()  # dpi of the current figure, not the saved image
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")

# Makes the image twice as big
F.set_size_inches((DefaultSize[0]*2, DefaultSize[1]*2))
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test2.png")

# Makes the image twice as big, making all the fonts and lines larger too
F.set_size_inches(DefaultSize)# reset the size
Size = F.get_size_inches()
print("Size in Inches", Size)
F.savefig("test3.png", dpi=(200)) # change the dpi

Output:

using MPL version: 3.6.2
DPI: 72.0
Default size in Inches [6. 4.]
Which should result in a 432 x 288 Image
Size in Inches [12.  8.]
Size in Inches [6. 4.]
  • Right click and open images in a new tab to see the full size

  • (432 x 288)

enter image description here

  • This image is increased 2x over the original (864 x 576)

enter image description here

  • Changing the dpi increased the image size (1200 x 800), the font size, and the line width.
    • (200 dpi / 72 dpi) * (432 x 288) = (1200 x 800)

enter image description here

Note:

  1. This answer combines all three images in one image file to see the difference in sizes.
added 58 characters in body
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
added 172 characters in body
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
Removed inline comments about the image size in pixels, as they don't match the current output. Added the comments above the corresponding printed output
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
added 46 characters in body
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
Rollback to Revision 9
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
Rollback to Revision 7
Source Link
Loading
Added links to the functions used
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
Rollback to Revision 6
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
Rollback to Revision 5
Source Link
Loading
Updated the answer for clarity. This is an attempt to clean up the question, answers, and remove duplicates. As per official matplotlib documentation, don't use pylab https://stackoverflow.com/a/51011921/7758804
Source Link
Trenton McKinney
  • 63.4k
  • 41
  • 172
  • 215
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
Fixed numerix code as numpy
Source Link
Atilla Ozgur
  • 14.8k
  • 3
  • 55
  • 75
Loading
added notes on comments and filmstrip function
Source Link
jfs
  • 417.9k
  • 211
  • 1k
  • 1.7k
Loading
Source Link
jfs
  • 417.9k
  • 211
  • 1k
  • 1.7k
Loading