如何向 3D 绘图添加标签

问题描述 投票:0回答:1

这是我的代码,我想改进我的图表

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Data
names = [
    "A", "B", "C", "D", "E", "F",
    "G", "H", "I", "J", "K", "L"
]
wins = [
 [0,14,20,24,29,33,39,39,39,39,39,39,39,39,39,39,39,39],
 [0,7,13,17,23,27,30,30,30,30,30,30,30,30,30,30,30,30],
 [0,5,8,11,15,16,19,19,19,19,19,19,19,19,19,19,19,19],
 [0,7,11,17,20,25,29,29,29,29,29,29,29,29,29,29,29,29],
 [0,9,14,22,29,36,42,42,42,42,42,42,42,42,42,42,42,42],
 [0,6,10,16,20,24,29,29,29,29,29,29,29,29,29,29,29,29],
 [0,7,13,20,26,31,34,34,34,34,34,34,34,34,34,34,34,34],
 [0,10,13,18,24,29,33,33,33,33,33,33,33,33,33,33,33,33],
 [0,5,11,13,16,21,26,26,26,26,26,26,26,26,26,26,26,26],
 [0,12,15,18,21,25,30,30,30,30,30,30,30,30,30,30,30,30],
 [0,9,11,12,17,20,26,26,26,26,26,26,26,26,26,26,26,26],
 [0,15,20,25,27,33,37,37,37,37,37,37,37,37,37,37,37,37],
]
weeks = [f"Week{i+1}" for i in range(18)]  # X-axis categories (nb of weeks)

# Convert names into numerical indices for the Y-axis
x = np.arange(len(weeks))      # X-axis 
y = np.arange(len(names))      # Y-axis 
wins_array = np.array(wins)    # wins accumulated per weeks and per names

# Create a 3D plot
fig = plt.figure(figsize=(18, 18))
ax = fig.add_subplot(111, projection='3d')

# Plot each line and shade the area below
for i, y_week in enumerate(y):
    ax.plot(x, wins_array[i], zs=y_week, zdir='x', label=weeks[i])
    # Add shaded area below the line
    ax.bar(x, wins_array[i], zs=y_week, zdir='x', alpha=0.1)

# Customize labels
#ax.set_ylabel('Weeks')           # Y-axis is weeks
ax.set_zlabel('Wins')             # Z-axis is wins
ax.set_xticks(y)                  # X-ticks are names
ax.set_xticklabels(names, rotation=45)  # Name labels on X-axis
ax.set_yticks(x)                  # Y-ticks are weeks
ax.set_yticklabels(weeks)         # Week labels on Y-axis

plt.show()

这是结果:结果 我有一个没有标签的 3D 绘图线

这是我失败的尝试得到的结果,标签到处都是

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Data
names = [
    "A", "B", "C", "D", "E", "F",
    "G", "H", "I", "J", "K", "L"
]
wins = [
    [0, 14, 20, 24, 29, 33, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39],
    [0, 7, 13, 17, 23, 27, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
    [0, 5, 8, 11, 15, 16, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19],
    [0, 7, 11, 17, 20, 25, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29],
    [0, 9, 14, 22, 29, 36, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42],
    [0, 6, 10, 16, 20, 24, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29],
    [0, 7, 13, 20, 26, 31, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34],
    [0, 10, 13, 18, 24, 29, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33],
    [0, 5, 11, 13, 16, 21, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    [0, 12, 15, 18, 21, 25, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
    [0, 9, 11, 12, 17, 20, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    [0, 15, 20, 25, 27, 33, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37],
]
weeks = [f"Week{i+1}" for i in range(18)]  # X-axis categories (nb of weeks)

# Convert names into numerical indices for the Y-axis
x = np.arange(len(weeks))      # X-axis 
y = np.arange(len(names))      # Y-axis 
wins_array = np.array(wins)    # wins accumulated per weeks and per names

# Create a 3D plot
fig = plt.figure(figsize=(18, 18))
ax = fig.add_subplot(111, projection='3d')

# Plot each line and add labels
for i, y_week in enumerate(y):
    ax.plot(x, wins_array[i], zs=y_week, zdir='x', label=weeks[i])
    # Add shaded area below the line
    ax.bar(x, wins_array[i], zs=y_week, zdir='x', alpha=0.1)
    # Add labels directly on the curves
    for j, my_label in enumerate(wins_array[i]):
        ax.text(i, my_label, y_week, f"{my_label}", color="red", fontsize=8, ha="center")

# Customize labels
ax.set_zlabel('Wins')              # Z-axis is wins
ax.set_xticks(y)                   # X-ticks are names
ax.set_xticklabels(names, rotation=45)  # Name labels on X-axis
ax.set_yticks(x)                   # Y-ticks are weeks
ax.set_yticklabels(weeks)          # Week labels on Y-axis

plt.show()

但是我希望能够在曲线上看到以下标签,我该如何改进我的代码预期

你能帮助我尝试不同的方法但没有成功吗?

python matplotlib 3d label line
1个回答
0
投票

您可以使用 Matplotlib 的

text()
函数在要显示值的坐标处添加文本

ax.text(x_coord, y_coord, z_coord, value, color='red', fontsize=10)
© www.soinside.com 2019 - 2024. All rights reserved.