Python matplotlib 3d 轴刻度、标签和 LaTeX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37711538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib 3d axes ticks, labels, and LaTeX
提问by Forklift17
I am running thissample script, with the following modifications:
我正在运行此示例脚本,并进行了以下修改:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
ax.set_xlabel('$X$', fontsize=20, rotation=150)
ax.set_ylabel('$Y$')
ax.set_zlabel(r'$\gamma$', fontsize=30, rotation=60)
ax.yaxis._axinfo['label']['space_factor'] = 3.0
plt.show()
How do I adjust the axis ticks to that of my choosing? I.e., how would I get the z-axis to only label 2, 0, and -2, and in the font size that I want? I know how to do this in 2D but not 3D.
The script above produces the following:
如何将轴刻度调整为我选择的刻度?即,我如何让 z 轴仅标记 2、0 和 -2,以及我想要的字体大小?我知道如何在 2D 但不知道如何在 3D 中做到这一点。
上面的脚本产生以下内容:
Why is the x-axis label distorted, which I wanted to do with this script, but not the z-axis label (gamma)? This does not make sense. I need this axis labeled in the Greek letter. How do I fix this?
为什么 x 轴标签扭曲了,我想用这个脚本来做,而不是 z 轴标签(伽玛)?这根本不符合逻辑。我需要用希腊字母标记这个轴。我该如何解决?
采纳答案by Serenity
How do I adjust the axis ticks to that of my choosing? I.e., how would I get the z-axis to only label 2, 0, and -2, and in the font size that I want? I know how to do this in 2D but not 3D.
如何将轴刻度调整为我选择的刻度?即,我如何让 z 轴仅标记 2、0 和 -2,以及我想要的字体大小?我知道如何在 2D 但不知道如何在 3D 中做到这一点。
You have to change properties of zticks
.
您必须更改zticks
.
Why is the x-axis label distorted, which I wanted to do with this script, but not the z-axis label (gamma)? This does not make sense. I need this axis labeled in the Greek letter. How do I fix this?
为什么 x 轴标签扭曲了,我想用这个脚本来做,而不是 z 轴标签(伽玛)?这根本不符合逻辑。我需要用希腊字母标记这个轴。我该如何解决?
You have to disable autorotation for z axis labels. Look at the code below:
您必须禁用 z 轴标签的自动旋转。看看下面的代码:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
ax.set_xlabel('$X$', fontsize=20)
ax.set_ylabel('$Y$')
ax.yaxis._axinfo['label']['space_factor'] = 3.0
# set z ticks and labels
ax.set_zticks([-2, 0, 2])
# change fontsize
for t in ax.zaxis.get_major_ticks(): t.label.set_fontsize(10)
# disable auto rotation
ax.zaxis.set_rotate_label(False)
ax.set_zlabel('$\gamma$', fontsize=30, rotation = 0)
plt.show()
回答by Paul Snijder
The for loop is not necessary, to change the size of your ticks you can use:
for 循环不是必需的,要更改刻度的大小,您可以使用:
ax.zaxis.set_tick_params(labelsize=10)