【问题标题】:label manipulation for 3d plot using matplotlib使用 matplotlib 进行 3d 绘图的标签操作
【发布时间】:2014-07-31 20:49:07
【问题描述】:

我想出了以下代码在 python+matplotlib 中生成一个图形:

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,1, projection='3d')

ax.plot_surface(KX[kxl3d:kxr3d,kxl3d:kxr3d], KY[kxl3d:kxr3d,kxl3d:kxr3d],
                BLP[kxl3d:kxr3d,kxl3d:kxr3d], rstride=8, cstride=8, alpha=0.4)

for idx in range(3):
    ax.plot(kx[x_points]+momentum_spi[idx,0], ky[y_points]+momentum_spi[idx,1],
            energy_spi[idx], linestyle='none', marker='o', 
            markerfacecolor=color_spi[idx], markersize=5)

ax.set_xlim(kl3d, kr3d)
ax.set_ylim(kl3d, kr3d)

ax.set_xlabel(r'$k_x[\mu m^{-1}]$')
ax.set_ylabel(r'$k_y[\mu m^{-1}]$')
ax.set_zlabel(r'$\epsilon-\omega_X[\gamma_p]$')

输出是:

我的问题是,我该怎么做

  1. 将 z 轴标签和刻度标签移动到图的左侧,这样它们就不会被圆环覆盖,并且
  2. 增加 x 和 y 刻度标签与轴标签之间的间距(注意它们有一点重叠,尤其是 y 轴)。

【问题讨论】:

  • 我认为您只是设置了错误的 xlim 和 ylim。这就是为什么你得到不愉快的输出。您可以发布完整的示例吗? (在当前 sn-p KX 中是未定义的)。

标签: python matplotlib plot


【解决方案1】:

官方的方法是使用:

  1. ax.tick_params(axis='z', pad=50)
  2. ax.set_zlabel(r'k_z...', labelpad=30)

不过,也有一点倒霉。 tick_params 1.3.1 的官方 API 文档说:虽然目前已实现此功能,但 Axes3D 对象的核心部分可能会忽略其中一些设置。似乎是 pad 是其中之一。

set_zlabel 的文档说:目前,labelpad 对标签没有影响。

第二个有一个简单的解决方法。它很丑但有效:

ax.set_zlabel('\n' + r'$\epsilon-\omega_X[\gamma_p]$', linespacing=2.5)

您可以通过调整linespacing 关键字来调整位置。单位是一行文本的高度。 (请注意,如果您进行交互式调整,除非您以某种方式更改文本,否则不会绘制它们。)

对于 z 刻度标签,您可以使用类似的技巧:

ax.set_zticklabels(["       "+tl.get_text() for tl in ax.get_zticklabels()])

这允许您一次调整一个空间的位置。当然,在数学文本模式下,您也可以使用间距命令 ('\/')。

【讨论】:

    【解决方案2】:

    您可以使用here 发布的代码将zaxis 向左对齐:

    tmp_planes = ax.zaxis._PLANES 
    ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3], 
                         tmp_planes[0], tmp_planes[1], 
                         tmp_planes[4], tmp_planes[5])
    view_1 = (25, -135)
    view_2 = (25, -45)
    init_view = view_2
    ax.view_init(*init_view)
    

    您可以控制标签到轴的距离,通过添加一个新行,并设置linespacing

    ax.set_xlabel('\n' + 'xlabel', linespacing=4)
    

    这是一个完整的例子:

    #!/usr/bin/python3
    
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    import numpy as np
    
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    tmp_planes = ax.zaxis._PLANES 
    ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3], 
                         tmp_planes[0], tmp_planes[1], 
                         tmp_planes[4], tmp_planes[5])
    view_1 = (25, -135)
    view_2 = (25, -45)
    init_view = view_2
    ax.view_init(*init_view)
    
    ax.plot_surface(X, Y, Z)
    
    ax.set_xlabel('\n' + 'xlabel', linespacing=4)
    ax.set_ylabel('ylabel')
    
    fig.tight_layout()
    
    fig.savefig('test.png')
    

    (请注意,zx 和 zy 网格位于框的错误一侧。我不知道如何解决)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2011-07-07
      • 2016-08-17
      相关资源
      最近更新 更多