我试图让y轴上的数字不会相互重叠,如下图所示。我使用matplotlib我的代码使用plt.tight_layout()命令在时间绘制所有8个图。我不确定修复底部四个图中重叠的最佳方法是什么。
import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(421)
a2 = fig.add_subplot(422)
a3 = fig.add_subplot(423)
a4 = fig.add_subplot(424)
a5 = fig.add_subplot(425)
a6 = fig.add_subplot(426)
a7 = fig.add_subplot(427)
a8 = fig.add_subplot(428)
a1.plot(Msol, ilP, color='blue')
a1.set_xlabel(r'$M/M\odot$')
a1.set_ylabel(r'$Log Pressure$')
a2.plot(Msol, ilT, color='blue')
a2.set_xlabel(r'$M/M\odot$')
a2.set_ylabel(r'$Log Temperature$')
a3.plot(Msol, ilRho, color='blue')
a3.set_xlabel(r'$M/M\odot$')
a3.set_ylabel(r'$Log Density$')
a4.plot(Msol, Rsol, color='blue')
a4.set_xlabel(r'$M/M\odot$')
a4.set_ylabel(r'$R/R\odot$')
a5.plot(Msol, Lsol, color='blue')
a5.set_xlabel(r'$M/M\odot$')
a5.set_ylabel(r'$L/L\odot$')
a6.plot(Msol, iK, color='blue')
a6.set_xlabel(r'$M/M\odot$')
a6.set_ylabel(r'$Opacity$')
a7.plot(Msol, ieg, color='blue')
a7.set_xlabel(r'$M/M\odot$')
a7.set_ylabel(r'$\epsilon$')
a8.plot(Msol, ir_c, color='blue')
a8.set_xlabel(r'$M/M\odot$')
a8.set_ylabel(r'$Convective Ratio$')
plt.tight_layout()
plt.show()
答案 0 :(得分:2)
使用get_ylim
,set_yticks
和set_fontsize
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot( np.random.randn( 1000 ).cumsum( ) )
lb, ub = ax.get_ylim( )
ax.set_yticks( np.linspace(lb, ub, 25 ) )
for x in ax.get_yticklabels( ):
x.set_fontsize( 'small' )
答案 1 :(得分:1)
根据this page,tight_layout
仅适用于图表的位置和填充,而不适用于刻度范围和数字。如你所知,你的图表很小,也许你应该自己adjust the number of ticks?