Python首先x-y-ticks重叠

时间:2016-04-02 11:54:14

标签: python matplotlib overlapping

我现在正在解决这个问题超过一个小时,但到目前为止,我所有尝试都失败了。 在我的绘图中,x和y轴上的第一个值保持重叠。我使用紧密的布局来解决这个问题,但它没有帮助。 我的z-ticks也与Z轴重叠。 谢谢你的任何建议 My plot

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)

1 个答案:

答案 0 :(得分:0)

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

一种。增强数字大小

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

enter image description here

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

enter image description here

B中。调整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])  

enter image description here

℃。调整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)