pandas 散点矩阵中的多个数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21131707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple data in scatter matrix
提问by Manuel
Is it possible to add multiple data to a pandas.tools.plotting.scatter_matrixand assigning a color to each group of data?
是否可以将多个数据添加到 apandas.tools.plotting.scatter_matrix并为每组数据分配颜色?
I'd like to show the scatter plots with data points for one group of data, let's say, in green and the other group in red in the very same scatter matrix. The same should apply for the density plots on the diagonal.
I know that this is possible by using matplotlib's scatterfunction, but that does not give me a scatter matrix.
我想显示散点图,其中包含一组数据的数据点,比方说,在同一个散点矩阵中,以绿色表示,另一组以红色表示。这同样适用于对角线上的密度图。我知道这可以通过使用 matplotlib 的scatter函数来实现,但这并没有给我一个散点矩阵。
The documentation of pandas is mum on that.
大Pandas的文档对此不以为然。
回答by neone4373
The short answer is determine the color of each dot in the scatter plot, role it into an array and pass it as the colorargument.
简短的回答是确定散点图中每个点的颜色,将其放入数组并将其作为color参数传递。
Example:
例子:
from pandas.tools.plotting import scatter_matrix
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
iris_data = pd.DataFrame(data=iris['data'],columns=iris['feature_names'])
iris_data["target"] = iris['target']
color_wheel = {1: "#0392cf",
2: "#7bc043",
3: "#ee4035"}
colors = iris_data["target"].map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(iris_data, color=colors, alpha=0.6, figsize=(15, 15), diagonal='hist')


回答by cmoi4lec
For me this answer didn't worked... But with this little correction it went well for me !
对我来说,这个答案没有用......但是通过这个小小的修正,它对我来说很顺利!
import pandas as pd
from pandas.plotting import scatter_matrix
from sklearn import datasets
iris = datasets.load_iris()
iris_data = pd.DataFrame(data=iris['data'],columns=iris['feature_names'])
iris_data["target"] = iris['target']
color_wheel = {1: "#0392cf",
2: "#7bc043",
3: "#ee4035"}
colors = iris_data["target"].map(lambda x: color_wheel.get(x + 1))
ax = scatter_matrix(iris_data, color=colors, alpha=0.6, figsize=(15, 15), diagonal='hist')

