简体   繁体   English

如何使用来自 3 列 numpy 数组(gpname,x,y)的组创建二维散点图?

[英]How to create a 2D scatterplot with groups from 3 columns numpy array (gpname, x,y)?

Python 2.7 Need your help with creating a 2D scatterplot from a Numpy array of 3 dimension where Col0 is used for Group Tag and Col 1 and 2 for the coordinates (X, Y). Python 2.7 需要您帮助从 3 维 Numpy 数组创建 2D 散点图,其中 Col0 用于组标签,Col 1 和 2 用于坐标(X,Y)。

Numpy array looks like below Numpy 数组如下所示

array([['A', '4.83186388889', '2.34534666667'],
   ['A', '4.87818611111', '2.80832888889'],
   ['A', '4.82518611111', '2.33834222222'],
   ['B', '4.53763888889', '-11.88424'],
   ['B', '4.503125', '-11.9406266667'],
   ['B', '4.45975555556', '-11.9688044444'],
   ['C', '6.12376666667', '-9.61480888889'],
   ['C', '6.20991666667', '-9.66523111111'],
   ['C', '6.12281388889', '-9.61702222222'],
   ['D', '6.46020833333', '-11.9756488889'],
   ['D', '6.43584166667', '-11.8586622222'],
   ['D', '6.43401111111', '3.88036888889'],
   ....
   dtype='|S21')

Dictionary cannot be used as it stores unique keys (groups) and I do not have an idea how to convert it into Pandas DataFrame with a proper format.无法使用字典,因为它存储唯一键(组),我不知道如何将其转换为具有正确格式的 Pandas DataFrame。

Tried like below previously and even though it was printed OK it did not work for the chart.以前像下面这样尝试过,即使打印正常,它也不适用于图表。

dataset = pd.DataFrame(**array**, columns = ['Description','X','Y'])
dataset[['X','Y']] = dataset[['X','Y']].apply(pd.to_numeric)

I'd like to create a 2D scatterplot for all my group tag's (A, B, C, ...) - of multiple sets of coordinates (x,y) - separate color per group (A, B, C, ...)我想为我的所有组标签 (A, B, C, ...) 创建一个二维散点图 - 多组坐标 (x,y) - 每组单独的颜色 (A, B, C, .. .)

Looking forward to your help.期待您的帮助。

You don't need pandas for plotting, just matplotlib.绘图不需要熊猫,只需要 matplotlib。 You can iterate over the array and pass each XY coordinate to plt.scatter .您可以遍历数组并将每个 XY 坐标传递给plt.scatter You could even use a structure (like a dictionary) where you define a specific color for each group:您甚至可以使用结构(如字典)为每个组定义特定颜色:

import matplotlib.pyplot as plt

colors = {'A': 'red', 
          'B': 'blue',
          'C': 'green',
          'D': 'black'}    
for group, x, y in array:
    plt.scatter(float(x), float(y), color=colors[group])
plt.show()

Edit: use this instead in order to dinamically create random colors for each group, no matter how many:编辑:使用它来为每个组动态创建随机颜色,无论有多少:

from random import random
import matplotlib.pyplot as plt

colors = {}    
for group, x, y in array:
    plt.scatter(float(x), float(y), color=colors.setdefault(group, (random(), random(), random())))
plt.show()

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

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