Search code examples
pythonmatplotlibsubplotsavefig

Why does the upper table overlap the lower figure-lines?


Question is a figure & table together from different axes.

On the one hand, its fine in figure window shows below, after constrained_layout=True is added:

enter image description here

On the other hand, the lower-lines and upper-table overlapped after save it to picture. plt.savefit("path+fig.png", bbox_index='tight)

enter image description here


Solution

  • I've learned how to avoid the problem that lines and tables overlapped each other.(yet not fully understand)

    plt.savefig('path+name.jpg', bbox_inches='tight')
    plt.tight_layout()
    

    instead of

    plt.tight_layout()
    plt.savefig('path+name.jpg', bbox_inches='tight')
    


    • If plt.tight_Layout() is before plt.savefig(), the result is lines are tight only, excluding tables(tables very likely will be overlapped), so does the pictures created by plt.savefig();
    • if we set plt.rcParams['figure.autolayout'] = True instead of plt.tight_layout(). Well, lines and tables is fine in the figure window, however, the tables are overlapped still in the picture created by plt.savefig();
    • if we move plt.savefig(),
    • plt.tight_layout() can only zoom the lines excluding tables;
    • plt.rcParams['figure.autolayout']=True can zoom both of lines and tables in the figure window.