saabiha
saabiha

Reputation: 3

How to change the plotting canvas size of the graph to 750 pixels using matplotlib in python?

I'm trying to adjust the size of the area where I'm plotting my line graph. I want just that area to be 750 pixels wide. All the solutions i have previously tried involve using plt.figure(figsize=) and all that changes is the overall size of the figure, not the canvas itself (the rectangle within which the curves are drawn). Below is a snippet of my code and resulting graph i get (the entire figure including the white space around the x and y axis is 727 pixels wide, i want just the area inside the axes to be 750 pixels wide)

code snippet

resulting graph

Upvotes: 0

Views: 1917

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

In order to have the axes have a certain size, a small calculation is necessary.

If target = 750 and dpi=100, and you want to have 10% margin on both sides, the total figure width needs to be

figwidth = target / dpi / (1.-2*0.1) = 9.375

You can do this calculation and set the respective numbers in the code

fig = plt.figure(figsize=(9.375, 5), dpi=100)
fig.subplots_adjust(left=0.1, right=0.9)

or use calculate the numbers on the fly,

target = 750
dpi=100
margin=0.1
fig = plt.figure(figsize=(target / (1.-2*margin) /dpi, 5), dpi=dpi)
fig.subplots_adjust(left=margin, right=1.-margin)

Upvotes: 1

Albo
Albo

Reputation: 1644

Don't use bbox_inches='tight', this leads to the wrong output. Just leave it be and it should work fine. I tried the following, and it worked like charm.

plt.figure(1, figsize=(7.5, 7.5), dpi=100)
plt.plot(A)
plt.savefig('Figure_1.png')
plt.show()

Look here for further information, there was already someone having the same problem.

Upvotes: 0

Related Questions