简体   繁体   English

如何在python中绘制多维数据点

[英]How to plot a multi-dimensional data point in python

Some background first: 一些背景优先:

I want to plot of Mel-Frequency Cepstral Coefficients of various songs and compare them. 我想绘制各种歌曲的Mel-Frequency Cepstral Coefficients并对它们进行比较。 I calculate MFCC's throughout a song and then average them to get one array of 13 coefficients. 我在一首歌中计算MFCC,然后将它们平均得到一个13个系数的数组。 I want this to represent one point on a graph that I plot. 我希望这能代表我绘制的图表上的一个点。

I'm new to Python and very new to any form of plotting (though I've seen some recommendations to use matplotlib). 我是Python新手,对任何形式的绘图都很陌生(虽然我已经看到了一些使用matplotlib的建议)。

I want to be able to visualize this data. 我希望能够可视化这些数据。 Any thoughts on how I might go about doing this? 关于我如何做到这一点的任何想法?

Firstly, if you want to represent an array of 13 coefficients as a single point in your graph, then you need to break the 13 coefficients down to the number of dimensions in your graph as yan king yin pointed out in his comment. 首先,如果你想在你的图表中将13个系数的数组表示为单个点,那么你需要将13个系数分解为图表中的维数,正如yan king yin在他的评论中指出的那样。 For projecting your data into 2 dimensions you can either create relevant indicators yourself such as max/min/standard deviation/.... or you apply methods of dimensionality reduction such as PCA. 要将数据投影到2维,您可以自己创建相关指标,例如最大/最小/标准偏差/ ....或者应用降维等方法,如PCA。 Whether or not to do so and how to do so is another topic. 是否这样做以及如何这样做是另一个主题。

Then, plotting is easy and is done as here: http://matplotlib.org/api/pyplot_api.html 然后,绘图很容易,并在此处完成: http//matplotlib.org/api/pyplot_api.html

I provide an example code for this solution: 我提供了此解决方案的示例代码:

import matplotlib.pyplot as plt
import numpy as np

#fake example data
song1 = np.asarray([1, 2, 3, 4, 5, 6, 2, 35, 4, 1])
song2 = song1*2
song3 = song1*1.5

#list of arrays containing all data
data = [song1, song2, song3]

#calculate 2d indicators
def indic(data):
    #alternatively you can calulate any other indicators
    max = np.max(data, axis=1)
    min = np.min(data, axis=1)
    return max, min

x,y = indic(data)
plt.scatter(x, y, marker='x')
plt.show()

The results looks like this: 结果如下: 在此输入图像描述

Yet i want to suggest another solution to your underlying problem, namely: plotting multidimensional data. 然而,我想为您的潜在问题提出另一种解决方案,即:绘制多维数据。 I recommend using something parralel coordinate plot which can be constructed with the same fake data: 我建议使用一些parralel坐标图,它可以使用相同的伪数据构建:

import pandas as pd
pd.DataFrame(data).T.plot()
plt.show()

Then the result shows all coefficents for each song along the x axis and their value along the y axis. 然后结果显示沿x轴的每首歌曲的所有系数和沿y轴的每个歌曲的所有系数。 I would looks as follows: 我看起来如下: 在此输入图像描述

UPDATE: 更新:

In the meantime I have discovered the Python Image Gallery which contains two nice example of high dimensional visualization with reference code: 与此同时,我发现了Python图像库 ,其中包含两个很好的高维可视化示例,其中包含参考代码:

在此输入图像描述

在此输入图像描述

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

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