简体   繁体   English

使用matplotlib对3d图的标签操作

[英]label manipulation for 3d plot using matplotlib

I came up with the following code to produce a figure in python+matplotlib: 我想出了以下代码来生成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]$')

The output is: 输出是: 3d_rings

My question is, how can I 我的问题是,我怎么能

  1. move the z axis label and tick labels to the left hand side of the figure, so that they are not overwritten by the rings and 将z轴标签和刻度标签移动到图的左侧,这样它们就不会被环和盖子覆盖
  2. increase the space between the x and y tick labels and the axes labels (notice they overlap a little, especially for the y axis). 增加x和y刻度标签与轴标签之间的空间(注意它们重叠一点,特别是对于y轴)。

You can snap zaxis to the left with the code posted 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)

You can control the distance of label to the axis, by adding a new line, and setting linespacing : 您可以通过添加新行和设置linespacing来控制标签到轴的距离:

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

Here's a complete example: 这是一个完整的例子:

#!/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')

在此输入图像描述

(Note though that the zx and zy grid goes on the wrong side of the box. I don't know how to fix that). (注意,虽然zx和zy网格在盒子的错误一侧。我不知道如何解决这个问题)。

The official way would be to use: 官方的方式是使用:

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

However, there is a bit of bad luck. 但是,运气不好。 With tick_params the official API documentation for 1.3.1 says: While this function is currently implemented, the core part of the Axes3D object may ignore some of these settings. 使用tick_params 1.3.1的官方API文档说: 虽然此函数当前已实现,但Axes3D对象的核心部分可能会忽略其中一些设置。 Seems to be that pad is one of these. 似乎pad是其中之一。

With set_zlabel the documentation says: Currently, labelpad does not have an effect on the labels. 使用set_zlabel文档说: 目前,labelpad对标签没有影响。

There is a simple workaround for the second one. 第二个有一个简单的解决方法。 It is ugly but works: 这很丑,但有效:

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

You may adjust the position by adjusting the linespacing keyword. 您可以通过调整linespacing关键字来调整位置。 The unit is the height of a row of text. 单位是一行文本的高度。 (Beware, if you do interactive adjustments, they are not drawn unless you change the text somehow.) (请注意,如果您进行交互式调整,除非您以某种方式更改文本,否则不会绘制它们。)

For the z tick labels you could use a similar trick: 对于z刻度标签,您可以使用类似的技巧:

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

This allows you to adjust the position one space at a time. 这允许您一次调整一个空间的位置。 Of course, in math text mode you may use the spacing commands as well ( '\\/' ). 当然,在数学文本模式中,您也可以使用间距命令( '\\/' )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM