matplotlib 3d 坐标轴刻度、标签和 LaTeX

2022-01-24 00:00:00 python matplotlib latex 3d mplot3d

问题描述

我正在运行

为什么我想用这个脚本做的 x 轴标签扭曲,而不是 z 轴标签 (gamma)?这根本不符合逻辑.我需要用希腊字母标记的这个轴.我该如何解决这个问题?

解决方案

如何将轴刻度调整为我选择的刻度?即,如何我将 z 轴仅标记为 2、0 和 -2,并且字体大小为我想?我知道如何在 2D 中做到这一点,但不是 3D.

您必须更改 zticks 的属性.

<块引用>

为什么x轴标签会扭曲,我想用这个来做脚本,但不是 z 轴标签(伽玛)?这根本不符合逻辑.一世需要用希腊字母标记这个轴.我该如何解决这个问题?

您必须禁用 z 轴标签的自动旋转.看下面的代码:

将 matplotlib 导入为 mpl从 mpl_toolkits.mplot3d 导入 Axes3D将 numpy 导入为 np将 matplotlib.pyplot 导入为 pltmpl.rcParams['legend.fontsize'] = 10无花果 = plt.figure()ax = fig.gca(投影='3d')theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)z = np.linspace(-2, 2, 100)r = z**2 + 1x = r * np.sin(θ)y = r * np.cos(theta)ax.plot(x, y, z, label='参数曲线')斧头传奇()ax.set_xlabel('$X$', fontsize=20)ax.set_ylabel('$Y$')ax.yaxis._axinfo['label']['space_factor'] = 3.0# 设置 z 刻度和标签ax.set_zticks([-2, 0, 2])# 更改字体大小对于 ax.zaxis.get_major_ticks() 中的 t:t.label.set_fontsize(10)# 禁用自动旋转ax.zaxis.set_rotate_label(False)ax.set_zlabel('$gamma$', fontsize=30, 旋转 = 0)plt.show()

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? 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.

  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)? 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? 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.

You have to change properties of 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?

You have to disable autorotation for z axis labels. 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()

相关文章