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)
0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

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?
Reduce the figure size?
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.
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))
I have tried all figsize, (2,2); (3x3), (5x5) and others, but the general size still the same
|
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.