0

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

  • if it's the basic canvas from tkinter you can do something like - name_of_canvas.config(width=750) – RAZ_Muh_Taz Sep 21 '17 at 18:10
  • Please, show a small code snippet and a print screen of what you have so far, and explain better what you expect. It will be easier to help you. – eguaio Sep 21 '17 at 18:18
  • Thanks, i have updated my question. Hopefully its a little bit more clear now – saabiha Sep 21 '17 at 18:48

2 Answers2

1

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)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

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.

Albo
  • 1,584
  • 9
  • 27
  • The solution you have provided increases the overall size of the figure. I was looking to just make the area inside the x and y axis( where the line graph is plotted) 750 pixels wide – saabiha Sep 21 '17 at 19:57
  • Oh, I'm sorry, my fault. But maybe the link helps you. – Albo Sep 21 '17 at 20:10
  • [This should help](https://stackoverflow.com/questions/13714454/specifying-and-saving-a-figure-with-exact-size-in-pixels). Though this again might just be the overall figure – Oli Sep 21 '17 at 20:54