python - 3D 绘制所有可能的排列或 0-9 的可能组合

标签 python matplotlib

我试图在 3D 图中绘制数字 0-9 的所有排列。到目前为止,我已经学会了足够的基础知识,但是我很难掌握绘制所有 X、Y、Z 值 0-9 的排列的概念。我确实理解码合与排列不同,但不确定组合是否更适合我正在尝试的内容。
到目前为止,我已经取得了以下解决方案。但不熟matplotlib和一般的Python。我的印象还在itertools可用于进行 0-9 的排列。但我不知道如何在这样的场景中实现它。
我想在 X、Y、Z 上进行这些排列,然后能够调用一个函数来获取 X、Y、Z 平面上的所有值,指定为仅 3 个值的参数,表示相交的排列数平面由这3个坐标。如果您要从 X、Y、Z 位置画一条线以获取排列值作为引用。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

i = 0
# Plotting (X, Y, Z) for range 0-9, but all permutations of (X, Y, Z) 0-9
for x, y, z in zip(range(0, 10), range(0, 10), range(0, 10)):
    i += 1  # Need to show number on plot
    # Plot point, but also display number
    ax.text(x, y, z, i, color="red")

# Set x limiter
ax.set_xlim(0, 10)
# Set y limiter
ax.set_ylim(0, 10)
# Set z limiter
ax.set_zlim(0, 10)

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

# Show plots
plt.show()

最佳答案

您的问题是 zip , 我认为。

i = 0
for x, y, z in zip(range(0, 10), range(0, 10), range(0, 10)):
    i += 1
    print(x, y, z, i)
0 0 0 1
1 1 1 2
2 2 2 3
3 3 3 4
4 4 4 5
5 5 5 6
6 6 6 7
7 7 7 8
8 8 8 9
9 9 9 10
迭代所有 x、y、z 组合的最简单方法可能是:
for x in range(0, 10):
    for y in range(0, 10):
        for z in range(0, 10):
            i += 1
但您可能也对 itertools.product 感兴趣
for x, y, z in itertools.product(range(0, 10), range(0, 10), range(0, 10)):
https://docs.python.org/3/library/itertools.html#itertools.product
如果你想禁止重复(即没有 <1,3,1>)https://docs.python.org/3/library/itertools.html#itertools.permutations可能会有所帮助

关于python - 3D 绘制所有可能的排列或 0-9 的可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63744544/

相关文章:

python - 直接从 CPU 读取 TEMPERATURE_TARGET

python - 应如何在 Tkinter 中显示可滚动的电子表格?

python - django 在同一模板中为不同的 for 循环重用 html block

python - matplotlib pyplot : subplot size

python - matplotlib 交互式绘图(在图表上手动绘制线条)

python - 如何从 C 扩展模块中的 __future__ 导入

python - 使用 pandas.DataFrame.plot : sort legend by value

python - Matplotlib:如何绘制按数字着色的网格框

python - 使用 matplotlib 在 python3 中为多个形状设置动画

python - 如何为 x 轴上的刻度设置动画?