当前位置:网站首页>PLT in Matplotlib tight_ layout()

PLT in Matplotlib tight_ layout()

2022-07-06 08:21:00 It's a dream, it's you!

Catalog

1 problem

2 resolvent  

3 Case study  

3.1 Code

3.2 result  

4 add plt.tight_layout() after

4.1 Code

4.2 result  


1 problem

(1) stay matplotlib in , Axis domain ( Including subgraphs ) The position of the is specified in standardized drawing coordinates . What may happen is , Your axis label or title ( Sometimes even scale labels ) It will exceed the graphic area , Therefore, it is truncated .

(2) When you have multiple subgraphs , You will often see labels of different axis fields stacked together .

2 resolvent  

# stay plt.show() with :
plt.tight_layout()

3 Case study  

3.1 Code

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib; matplotlib.use('TkAgg')
mpl.rcParams['font.sans-serif'] = ['SimHei']  #  Specify default font 
mpl.rcParams['axes.unicode_minus'] = False  #  Resolve save image is negative '-' Questions displayed as squares 
plt.rcParams['savefig.facecolor'] = "0.8"

def example_plot(ax, fontsize=12):
     ax.plot([1, 2])
     ax.locator_params(nbins=3)
     ax.set_xlabel('x-label', fontsize=fontsize)
     ax.set_ylabel('y-label', fontsize=fontsize)
     ax.set_title('Title', fontsize=fontsize)

plt.close('all')
plt.close('all')
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.show()

3.2 result  

4 add plt.tight_layout() after

4.1 Code

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib; matplotlib.use('TkAgg')
mpl.rcParams['font.sans-serif'] = ['SimHei']  #  Specify default font 
mpl.rcParams['axes.unicode_minus'] = False  #  Resolve save image is negative '-' Questions displayed as squares 
plt.rcParams['savefig.facecolor'] = "0.8"

def example_plot(ax, fontsize=12):
     ax.plot([1, 2])
     ax.locator_params(nbins=3)
     ax.set_xlabel('x-label', fontsize=fontsize)
     ax.set_ylabel('y-label', fontsize=fontsize)
     ax.set_title('Title', fontsize=fontsize)

plt.close('all')
plt.close('all')
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout()
plt.show()

4.2 result  

 

 

 

原网站

版权声明
本文为[It's a dream, it's you!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131832397085.html