87

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()
1
  • Does changing the extension in the file name from .png into .tif create the real tiff image? Commented Jun 14, 2013 at 14:03

4 Answers 4

159

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

Sign up to request clarification or add additional context in comments.

3 Comments

Does changing the extension in the file name from .png into .tif create the real tiff image?
As I wrote, I don't know, but from the comments on the other answer it seems that tiff is non supported
Keep in mind plt.figure(figsize=(3,4)) needs to be called before calling plt.plot()
66

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

plt.rcParams["figure.figsize"] = [16,9]

7 Comments

This is just what I needed to use in Jupyter Notebook to make the plot wider.
Working in jupyter notebooks
rcParams has enormous power of customizability
I used this and my image got stretched, matplotlib did not adjusted the content for the new aspect ratio. However if I do not save but show the plot on a window, it works. Any clues on that?
Pl. use some other aspect ratios (like: 4,3) and update me.
|
27

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 ;-)

2 Comments

If you have the GDK backend installed you can use it to draw tiffs natively. There may be some dependencies you have to install.
@GWW -- I saw that mentioned, but I also saw this thread implying that is a lie. Of course, it's an old thread, so it may have been fixed since then.
24

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.

2 Comments

This is increasing only the figure, not the window containing it.
matplotlib has one of the worst APIs ever, the trouble is, it is the only easiest to learn tool in python.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.