【问题标题】:Group scatter points by a certain condition and make a legend from it按特定条件对散点进行分组并从中制作图例
【发布时间】:2019-11-16 12:34:39
【问题描述】:

我有许多数据想用 3D 的每个组合绘制。所以数据看起来像这样。

我创建了一个矩阵:

a = array([[1,2,2],[3,4,3],[4,2,2],[2,7,4],[5,8,1],[7,1,7]])

我将跳过这个例子的循环。

k = 1 ; j = 0; z = 2
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a[:,j], a[:,z], a[:,k])

下一步是我想按 A 列(如果它们属于 Random1 或 Random2)为图上色,并根据此条件制作一个图例。它不起作用。

我的尝试是列出 A 列

a = [Random1, Random2]

并将其放入我的图形绘图中。

k = 1 ; j = 0; z = 2
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a[:,j], a[:,z], a[:,k], label = a)
ax.legend()

基本上我想要这样的东西scatter plot with legend colored by group without multiple calls to plt.scatter

但使用 matplotlib(它是必需的)和 3d。 希望它清楚我想要做什么。

编辑: 有了这个添加,它确实可以按照我的意愿为它们着色,但仍然不知道根据颜色制作图例的人。

ax.scatter(a[:,j], a[:,z], a[:,k], c = len, cmap = 'brg'))

其中 len 是我的数据长度。

我可以使用 pandas 之类的东西来为相应的数据添加标签,但从矩阵中读取数据吗?

【问题讨论】:

  • 您想将矩阵a 的每一行绘制为点吗?
  • 积分是什么意思?这不是将它们绘制成不连接它们的点吗?这只是一个例子。我的数据中有大约 10000 个点和大约 30 组随机数。
  • 或类似的东西,但同样使用 matplot lib 和 3d。 - stackoverflow.com/questions/44595288/…
  • 我可以使用 pandas 之类的东西来添加标签吗?
  • 您想绘制每一行或所有可能的组合?从您使用的循环中,我相信您需要所有组合,但是标签没有意​​义。第 1 行有标签 Random1,但如果你混合这些点,标签是什么?

标签: python matplotlib


【解决方案1】:

我正在更新我的答案。因此,根据您所说的,您想要绘制所有可能的点组合。

这应该可以正常工作

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from numpy import array
import matplotlib.patches as mpatches

a = array([[1,2,2],[3,4,3],[4,2,2],[2,7,4],[5,8,1],[7,1,7]])

colors = ['r', 'r','r','g','g','g']

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for i in range(3):
    for j in range(3):
        for k in range(3):
            ax.scatter(a[:,i], a[:,j], a[:,k], c=colors)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

R1 = mpatches.Patch(color='red', label='Random 1')
R2 = mpatches.Patch(color='green', label='Random 2')
plt.legend(handles=[R1,R2])
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多