2

Could someone tell me, why the size of the figure generated by the code below is not as expected.

How can I modify this code to generate a picture with 100*400 pixels?

import numpy as np
import matplotlib.pyplot as plt

fig  = plt.figure(figsize=(1,4),dpi=100,facecolor = 'red')
plt.show(fig)

screenshot

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Sean Liu
  • 21
  • 1
  • 1
  • 2
  • the picture generated by this code have 218*409 pixels, and I don't konw why? – Sean Liu Mar 11 '16 at 06:17
  • Good point. I tried (very small changes to your code) and got 100dpi and 120x398 pixels. Size is in inches and dpi is dots per inch, so you should get 100 x 400. – roadrunner66 Mar 11 '16 at 06:20
  • But see this page: `http://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib` So this is a possible duplicate. – roadrunner66 Mar 11 '16 at 06:26

1 Answers1

1

You should set the dpi during savefig

import numpy as np
import matplotlib.pyplot as plt

fig  = plt.figure(figsize=(1,4),facecolor = 'red', dpi=100)
plt.savefig('test.png', dpi=100)

plt.show(fig)

Of course, it wont be perfect, but will be close ..

ssm
  • 5,277
  • 1
  • 24
  • 42