简体   繁体   English

matplotlib 3d轴刻度,标签和LaTeX

[英]matplotlib 3d axes ticks, labels, and LaTeX

I am running this sample 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()
  1. How do I adjust the axis ticks to that of my choosing? 如何将轴刻度调整为我选择的轴刻度? Ie, how would I get the z-axis to only label 2, 0, and -2, and in the font size that I want? 即,我如何让z轴仅标记为2,0和-2,以及我想要的字体大小? I know how to do this in 2D but not 3D. 我知道如何在2D而不是3D中执行此操作。

  2. The script above produces the following: 上面的脚本产生以下内容:

在此输入图像描述

Why is the x-axis label distorted, which I wanted to do with this script, but not the z-axis label (gamma)? 为什么x轴标签失真,我想用这个脚本做,但不是z轴标签(gamma)? This does not make sense. 这根本不符合逻辑。 I need this axis labeled in the Greek letter. 我需要在希腊字母中标注这个轴。 How do I fix this? 我该如何解决?

How do I adjust the axis ticks to that of my choosing? 如何将轴刻度调整为我选择的轴刻度? Ie, how would I get the z-axis to only label 2, 0, and -2, and in the font size that I want? 即,我如何让z轴仅标记为2,0和-2,以及我想要的字体大小? I know how to do this in 2D but not 3D. 我知道如何在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)? 为什么x轴标签失真,我想用这个脚本做,但不是z轴标签(gamma)? This does not make sense. 这根本不符合逻辑。 I need this axis labeled in the Greek letter. 我需要在希腊字母中标注这个轴。 How do I fix this? 我该如何解决?

You have to disable autorotation for z axis labels. 您必须为z轴标签禁用自动旋转。 Look at the code below: 看下面的代码:

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()

在此输入图像描述

for循环不是必需的,可以改变你可以使用的刻度的大小:

    ax.zaxis.set_tick_params(labelsize=10)

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

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