ValueError when using matplotlib tight_layout ()

Ok, this is my first question asking a question here, so please be patient with me; -)

I am trying to create a series of subheadings (with two y-axes each) on a figure using matplotlib and then save that figure. I use GridSpec to create a grid for the subtitles and realized that they overlap a bit, which I don't want. So I'm trying to use tight_layout () to figure it out, which, according to the matplotlib documentation, should work fine. To simplify some things, my code looks something like this:

import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure(num=None, facecolor='w', edgecolor='k') grid = gridspec.GridSpec(2, numRows) # numRows comes from the number of subplots required # then I loop over all the data files I'm importing and create a subplot with two y-axes each time ax1 = fig.add_subplot(grid[column, row]) # now I do all sorts of stuff with ax1... ax2 = ax1.twinx() # again doing some stuff here 

After the data processing cycle is completed and I have created all the subheadings, I end up with

 fig.tight_layout() fig.savefig(str(location)) 

As far as I can do this, this should work, however, when you call tight_layout (), I get a ValueError from the self.subplotpars: left function cannot be> = right. My question is: how to find out what causes this error and how to fix it?

+7
python matplotlib
source share
1 answer

I had this error before, and I have a solution that worked for me. I'm not sure this will work for you. The matplotlib command

 plt.fig.subplots_adjust() 

can be used to sort the plot. The left and bottom stretch more, the smaller the number, and the upper and right stretch more, the larger the number. Thus, if left is greater than or equal to right, or a lower value is greater than or equal to an upper value than the graph will discard. I adjusted my command to look like this:

 fig = plt.figure() fig.subplots_adjust(bottom = 0) fig.subplots_adjust(top = 1) fig.subplots_adjust(right = 1) fig.subplots_adjust(left = 0) 

You can then fill in your own numbers to set it up if you keep the left and bottom smaller. I hope this solves your problem.

+2
source share

All Articles