简体   繁体   English

Seaborn和pd.scatter_matrix()绘制颜色问题

[英]Seaborn and pd.scatter_matrix() plot color issues

I am making a pd.scatter_matrix() plot from a DataFrame based on the Iris dataset colored by the target variable (plant species). 我正在根据目标变量(植物种类)着色的虹膜数据集,从DataFrame绘制pd.scatter_matrix()图。 When I run the code below I get a scatter matrix with black, grey and white (!) colored scattering points which hinders visualization. 当我运行下面的代码时,我得到了具有黑色,灰色和白色(!)色散射点的散射矩阵,这阻碍了可视化。 The grid seems inconsistent too, apparently only the plots close to the axis get the respective gridding. 网格似乎也不一致,显然只有靠近轴的图才获得相应的网格。 I wanted a nice grid and scatter matrix following the sns default color palette (blue, green, red). 我想要一个遵循sns默认调色板(蓝色,绿色,红色)的漂亮的网格和散布矩阵。

Why is seaborn plot style and the use of pd.scatter_matrix() enforcing a different (awful!) color palette then the defaults for the scatter plots and inconsistent grid lines? 为什么seaborn绘图样式和使用pd.scatter_matrix()强制使用不同的(糟糕的!)调色板,而不使用散点图的默认值和不一致的网格线? How can I solve these visualization issues? 如何解决这些可视化问题?

I already updated seaborn to a fairly recent version (0.8 of July 2017). 我已经将seaborn更新为相当新的版本(2017年7月0.8版)。 Also tried the non-deprecated version the scatter_matrix plot for pandas pd.plotting.scatter_matrix() and had no luck. 还尝试了不推荐使用的版本pandas pd.plotting.scatter_matrix()的scatter_matrix图,但是没有运气。 If I use the 'ggplot' style the color palette is correct for the scatter plots but the grids are still inconsistent. 如果我使用“ ggplot”样式,则调色板对于散点图是正确的,但网格仍然不一致。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn')
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target
df = pd.DataFrame(X, columns = iris.feature_names)

pd.scatter_matrix(df, c=y, figsize = [8,8],
                      s=80, marker = 'D');

在此处输入图片说明

Package versions: 套件版本:

pandas version: 0.20.1 熊猫版:0.20.1
matplotlib version: 2.0.2 matplotlib版本:2.0.2
seaborn version:0.8.0 海上版本:0.8.0

I am not sure if this answers your question but you could use the pairplot. 我不确定这是否能回答您的问题,但是您可以使用pairplot。 let me know.. 让我知道..

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target
df = pd.DataFrame(X, columns = iris.feature_names)

pd.plotting.scatter_matrix(df, c=y, figsize = [8,8],
                      s=80, marker = 'D');
df['y'] = y

sns.pairplot(df,hue='y')

which gives you: 这给你:

在此处输入图片说明

If you want to avoid that the last line of the visualizations then: 如果要避免可视化的最后一行,则:

import seaborn as sns
sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
%matplotlib inline

iris = sns.load_dataset("iris")
sns.pairplot(iris, hue="species")

在此处输入图片说明

Default matplotlib setting are not very aesthetic; 默认的matplotlib设置不是很美观。 however, do not underestimate the power of matplotlib . 但是,请不要低估matplotlib

The simplest solution to your problem might be: 解决问题的最简单方法可能是:

plt.style.use('ggplot') # this is the trick

from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target
df = pd.DataFrame(X, columns = iris.feature_names)

pd.scatter_matrix(df, c=y, figsize = [10,10], s=50);

在此处输入图片说明

(full list of styles available can be accessed via plt.style.available ) (可以通过plt.style.available访问可用样式的完整列表)

You may further customize the plot to your needs adjusting matplotlibrc file. 您可以根据需要进一步调整绘图,以调整matplotlibrc文件。 An example of what could be done with it could be found here 这里可以找到如何处理的示例

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

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