如何旋转matplotlib中的ticklabel_format?

时间:2019-04-24 14:05:51

标签: python matplotlib

我正在使用matplotlib绘制图形,并且想旋转zaxis.set_major_formatter的方向。如图中带红色标记的图像所示。我怎么旋转它?

Fontsize_set = {'size' : 20}
fig = plt.figure(figsize=[8, 5], dpi = 140, facecolor = None)
ax = Axes3D(fig)
ax.grid(color = 'y', linestyle = ':', linewidth = 0.5)

## axes tick setting.
ax.tick_params(labelsize = 20)
ax.set_xlim3d(0, 1500)
ax.set_xticks(np.arange(0, 1500, 1500 / 5))
ax.set_ylim3d(0, 250)
ax.set_yticks(np.arange(0, 250, 250 / 5))
ax.set_zlim3d(0, 0.025)
ax.set_zticks(np.arange(0, 0.025, 0.025 / 5))

ax.view_init(20, 60)

ax.zaxis.set_rotate_label(False)     ## rotate the z axis.
ax.zaxis.set_major_formatter(plt.ScalarFormatter(useMathText = True))
ax.ticklabel_format(style = "sci",  axis = "z", scilimits = (-3, -3))

ax.xaxis.pane.set_edgecolor('k')
ax.yaxis.pane.set_edgecolor('k')
ax.zaxis.pane.set_edgecolor('k')
ax.xaxis.pane.set_alpha(1)
ax.yaxis.pane.set_alpha(1)
ax.zaxis.pane.set_alpha(1)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False

fig.tight_layout()
plt.show()

如下图所示:

enter image description here

Fontsize_set = {'size' : 20}
fig = plt.figure(figsize=[8, 5], dpi = 140, facecolor = 'w')
ax = plt.axes(projection='3d')
ax.grid(color = 'y', linestyle = '--', linewidth = 0.5)
## axes tick setting.
ax.tick_params(labelsize = 20)

ax.set_xlim3d(0, 2000)
ax.set_xticks(np.arange(0, 2000, 2000 / 5))

ax.set_ylim3d(0, 250)
ax.set_yticks(np.arange(0, 250, 250 / 5))

ax.set_zlim3d(0, 0.0025)
ax.set_zticks(np.arange(0, 0.0025, 0.0025 / 5))

ax.zaxis.set_major_formatter(plt.ScalarFormatter(useMathText = True))
ax.ticklabel_format(style = "sci", axis = "z", scilimits = (-4, -4))

ax.view_init(20, 60)
ax.zaxis.set_rotate_label(False)     ## rotate the z label.

fig.canvas.draw()
ax.zaxis.offsetText.set_visible(False)
ax.text2D(0.05, 0.85, ax.zaxis.offsetText.get_text(),
          transform = ax.transAxes, fontsize = 10)
ax.xaxis.pane.set_edgecolor('k')
ax.yaxis.pane.set_edgecolor('k')
ax.zaxis.pane.set_edgecolor('k')
ax.xaxis.pane.set_alpha(1)
ax.yaxis.pane.set_alpha(1)
ax.zaxis.pane.set_alpha(1)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
plt.tight_layout()
plt.show()

enter image description here

Fontsize_set = {'size' : 20}
fig = plt.figure(figsize=[8, 5], dpi = 140, facecolor = 'w')
ax = plt.axes(projection='3d')
ax.grid(color = 'y', linestyle = '--', linewidth = 0.5)
## axes tick setting.
ax.tick_params(labelsize = 20)

ax.set_xlim3d(0, 2000)
ax.set_xticks(np.arange(0, 2000, 2000 / 5))

ax.set_ylim3d(0, 250)
ax.set_yticks(np.arange(0, 250, 250 / 5))

ax.set_zlim3d(0, 0.0025)
ax.set_zticks(np.arange(0, 0.0025, 0.0025 / 5))

ax.zaxis.set_major_formatter(plt.ScalarFormatter(useMathText = True))
ax.ticklabel_format(style = "sci", axis = "z", scilimits = (-4, -4))

ax.view_init(20, 60)
ax.zaxis.set_rotate_label(False)     ## rotate the z label.

ax.zaxis.offsetText.set_visible(False)
ax.text2D(0.05, 0.85, r'$\times$$10^{-3}$',
          transform = ax.transAxes, fontsize = 20)
ax.xaxis.pane.set_edgecolor('k')
ax.yaxis.pane.set_edgecolor('k')
ax.zaxis.pane.set_edgecolor('k')
ax.xaxis.pane.set_alpha(1)
ax.yaxis.pane.set_alpha(1)
ax.zaxis.pane.set_alpha(1)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
plt.tight_layout()
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

在对制表符补全和matplotlib文档进行了大量试验之后,我只能提出以下替代方案。这个想法很简单:

  • 隐藏指数项offsetText
  • 将其替换为水平布局中的简单文本。

文本0.05, 0.85,的位置是一个选择问题。请注意,在我的系统上,我得到-2的指数,而你得到-3。但这并不重要,因为相应地缩放了z-ticklabel。

fig.canvas.draw()
ax.zaxis.offsetText.set_visible(False)
ax.text2D(0.05, 0.85, ax.zaxis.offsetText.get_text(), 
          transform=ax.transAxes, fontsize=20)

enter image description here