Sean Liu
Sean Liu

Reputation: 21

How to set the size of a figure in Python matplotlib

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

Upvotes: 2

Views: 14776

Answers (1)

ssm
ssm

Reputation: 5383

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

Upvotes: 1

Related Questions