Python 第一个 x-y 刻度重叠

Python first x-y-ticks overlapping

我现在已经在这个问题上工作了一个多小时,但到目前为止我所做的一切都失败了。 在我的图中,x 轴和 y 轴上的第一个值保持重叠。我正在使用紧凑的布局来解决这个问题,但它没有帮助。 我的 z-ticks 也与 Z 轴重叠。 感谢您的任何建议

font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 18}

matplotlib.rc('font', **font)
time=round(t[time_period],0)
fig = plt.figure(figsize=(10,5))
###first subplot  
ax = fig.add_subplot(1, 2, 1, projection='3d')
surf=ax.plot_surface(X_MESH, Y_MESH, Meshgrid_Output, rstride=1, cstride=1, cmap=cm.jet,linewidth=0, antialiased=False)
ax.set_xlabel(name+"$_"+str(most_sensitive[0])+" in "+str(unit)+"$")
ax.set_ylabel(name+"$_"+str(most_sensitive[1])+"$ in "+str(unit))
ax.set_zlabel("$\Delta$Output in [C]")  
##formating labels
ax.xaxis._axinfo['label']['space_factor'] = 4.2
ax.yaxis._axinfo['label']['space_factor'] = 4.2
ax.zaxis._axinfo['label']['space_factor'] = 3
##position and rotation ticks
ax.xaxis._axinfo['tick']['inward_factor'] = 0
ax.xaxis._axinfo['tick']['outward_factor'] = 0.4
ax.yaxis._axinfo['tick']['inward_factor'] = 0
ax.yaxis._axinfo['tick']['outward_factor'] = 0.4
ax.zaxis._axinfo['tick']['inward_factor'] = 0
ax.zaxis._axinfo['tick']['outward_factor'] = 0.4
ax.zaxis._axinfo['tick']['outward_factor'] = 0.4
plt.xticks(rotation=45)
plt.yticks(rotation=325)
ax.grid(False)
## setting background
ax.xaxis.pane.set_edgecolor('black')
ax.yaxis.pane.set_edgecolor('black')
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
#set nubmer of ticks
ab, bc = ax.get_xlim( )
ax.set_xticks( np.linspace(ab, bc, 4 ) )
cd, de = ax.get_ylim( )
ax.set_yticks( np.linspace(cd, de, 4 ) )
lb, ub = ax.get_zlim( )
ax.set_zticks( np.linspace(lb, ub, 5 ) )
##round values
ax.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
##set point of view
angle=132
ax.view_init(30, angle)

##second subplot
ax = fig.add_subplot(1, 2, 2)
cax=ax.imshow(Meshgrid_Output, extent=[Y_MESH.min(),Y_MESH.max(),X_MESH.max(),X_MESH.min()],aspect='auto',interpolation='nearest',cmap=cm.jet)
ax.set_xlabel(name+"$_"+str(most_sensitive[0])+"$ in "+str(unit))
ax.set_ylabel(name+"$_"+str(most_sensitive[1])+"$ in "+str(unit))
#set nubmer of ticks
ab, bc = ax.get_xlim( )
ax.set_xticks( np.linspace(ab, bc, 4) )
cd, de = ax.get_ylim( )
ax.set_yticks( np.linspace(cd, de, 4 ) )
##round values
ax.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3f'))
#postion ticks
ax.yaxis.tick_right()
ax.xaxis.set_label_position('top')
ax.grid(True)
## formation colorbar
cbar=fig.colorbar(cax,orientation='horizontal',aspect=20,pad=0.08)
cbar.locator = ticker.MaxNLocator(nbins=6)
cbar.update_ticks()
#Posion both subplots
fig.subplots_adjust(bottom=0.2)
fig.subplots_adjust(wspace=0.3)

这里我提供三种方法,也许会有帮助。

一个。提高身材尺寸

之前
fig = plt.figure()
ax1 = plt.subplot(121,projection='3d')
ax2 = plt.subplot(122)

设置figsize
fig = plt.figure(figsize=(16,3))
ax1 = plt.subplot(121,projection='3d')
ax2 = plt.subplot(122)

乙。调整ax1.subplot

的图形大小

使用gridspec更改ax1.subplot的部分。

import mpl_toolkits.mplot3d.axes3d as axes3d
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize = (12,6))
gs = gridspec.GridSpec(1, 2,
                       width_ratios=[6,1],
                       height_ratios=[1,1]
                       )
ax1 = plt.subplot(gs[0],projection='3d')
ax2 = plt.subplot(gs[1])  

C。调整 x/y ticklabel 的频率。

下面的代码可以调整xtick频率。使用较少的 xticks 及其标签,重叠可能会消失。

ax.set_xticks(np.arange(0,1,1,0.1))
for xtick in ax.xaxis.get_ticklines()[1::2]: ### Hiding ticks each 2 steps.
    xtick.set_visible(False)     
ax.set_xticklabels(np.arange(0,1,1,0.2),fontsize = 14)