1

I'm trying to remove the white space from the plot that I created:

enter image description here

As it is possible to see, there a big white spot on the right and also on the bottom, how to fix it? Here is my script:

fig = plt.figure(figsize=(7,7))
        
        
ax1 = plt.subplot2grid((4,3), (0,0),)
ax2 = plt.subplot2grid((4,3), (1,0),)
ax3 = plt.subplot2grid((4,3), (0,1),)
ax4 = plt.subplot2grid((4,3), (1,1),)
        
data = self.dframe[i]
        
tes = print_data(data, self.issues, self.color, self.type_user)
    
tes.print_top(data=data, top=10, ax=ax1, typegraph="hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax2, typegraph="prod_bar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax3, typegraph="reg_hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax4, typegraph=self.type_user, problem=self.issues[i], tone=self.color[i])
        
problem = self.issues[i]
plt.tight_layout()
name = problem + str('.PNG')
plt.close(fig)
fig.savefig(name)
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

8

You are creating too many subplots!

If we look at this line:

ax1 = plt.subplot2grid((4,3), (0,0),)

We can see the first argument given to subplot2grid are the dimensions of the subplot grid to be made, in this case 4 rows, and 3 columns. You are then plotting in the subplots in the top left of your figure (the second argument given) which leaves a lot of space that's not used.

So to solve this, reduce the number of subplots by using:

ax1 = plt.subplot2grid((2,2), (0,0),)

Full example:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(25)

fig = plt.figure(figsize=(7,7))

ax1 = plt.subplot2grid((2,2), (0,0),)
ax2 = plt.subplot2grid((2,2), (1,0),)
ax3 = plt.subplot2grid((2,2), (0,1),)
ax4 = plt.subplot2grid((2,2), (1,1),)

ax1.plot(data)
ax2.plot(data)
ax3.plot(data)
ax4.plot(data)

plt.show()

Giving:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • Thank you your help @DavidG, the plots filled all the empty spots, however, the picture is too big now, there is any other way which I can keep my original size and cut the empty spots? – Rafael Rodrigues Santos Jan 04 '18 at 16:20
  • Reduce the figure size? – DavidG Jan 04 '18 at 16:29
  • How do I do that?, I will use this plot to insert the picture saved (plot) into a widget using tkinter, I tried to reduce the figure saved size, however, the picture is not very visible there, that is why I want to cut the empty spots. – Rafael Rodrigues Santos Jan 04 '18 at 16:33
  • 1
    So you have removed the empty plots. However, the figure size has not changed, which means the existing 4 plots will be automatically scaled to fill the space. To reduce the overall size of the figure simply do `fig = plt.figure(figsize=(5,5))` – DavidG Jan 04 '18 at 16:35
  • I have tried all figsize, (2,2); (3x3), (5x5) and others, but the general size still the same – Rafael Rodrigues Santos Jan 04 '18 at 16:58
  • This is probably due to the way you are putting it into your tkinter widget. Probably best to ask a separate question. – DavidG Jan 04 '18 at 16:59
  • No problem :) as this answered your original question of removing the empty space in the figure it might be good to accept this answer. – DavidG Jan 04 '18 at 17:02
  • It worked, I have a question related, but not being solved yet. Could you please take a look, I don't know how to privately message you. Thanks.https://stackoverflow.com/q/48225888/2525479 – StayFoolish Jan 13 '18 at 09:44
1

you can use plt.subplots_adjust(left=0.09, bottom=0.07, right=0.98, top=0.97, wspace=0.2 , hspace=0.17 ) to adjust the window. But the issue is that a lot of the space in your plot is empty maybe you should change plt.subplot2grid((4,3)... to plt.subplot2grid((2,2)

ymmx
  • 4,769
  • 5
  • 32
  • 64