86

I want to obtain fig1 exactly of 4 by 3 inch sized, and in tiff format correcting the program below:

import matplotlib.pyplot as plt

list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]

plt.plot(list1, list2)
plt.savefig('fig1.png', dpi = 300)
plt.close()
bad_coder
  • 11,289
  • 20
  • 44
  • 72
golay
  • 941
  • 1
  • 7
  • 5
  • Does changing the extension in the file name from .png into .tif create the real tiff image? – golay Jun 14 '13 at 14:03

4 Answers4

158

You can set the figure size if you explicitly create the figure with

plt.figure(figsize=(3,4))

You need to set figure size before calling plt.plot() To change the format of the saved figure just change the extension in the file name. However, I don't know if any of matplotlib backends support tiff

E A
  • 995
  • 1
  • 10
  • 24
Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
65

You can change the size of the plot by adding this before you create the figure.

plt.rcParams["figure.figsize"] = [16,9]
Nyps
  • 831
  • 1
  • 14
  • 26
ghosh'.
  • 1,567
  • 1
  • 14
  • 19
26

The first part (setting the output size explictly) isn't too hard:

import matplotlib.pyplot as plt
list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]
fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(111)
ax.plot(list1, list2)
fig.savefig('fig1.png', dpi = 300)
fig.close()

But after a quick google search on matplotlib + tiff, I'm not convinced that matplotlib can make tiff plots. There is some mention of the GDK backend being able to do it.

One option would be to convert the output with a tool like imagemagick's convert.

(Another option is to wait around here until a real matplotlib expert shows up and proves me wrong ;-)

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • If you have the [GDK backend](http://matplotlib.org/faq/usage_faq.html#what-is-a-backend) installed you can use it to draw tiffs natively. There may be some dependencies you have to install. – GWW Jun 14 '13 at 13:34
  • @GWW -- I saw that mentioned, but I also saw [this thread](http://matplotlib.1069221.n5.nabble.com/16bit-tiff-support-td11448.html) implying that is a lie. Of course, it's an old thread, so it may have been fixed since then. – mgilson Jun 14 '13 at 13:36
23

If you need to change the figure size after you have created it, use the methods

fig = plt.figure()
fig.set_figheight(value_height)
fig.set_figwidth(value_width)

where value_height and value_width are in inches. For me this is the most practical way.

Antoni
  • 2,542
  • 20
  • 21