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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46383645/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:31:24  来源:igfitidea点击:

Seaborn and pd.scatter_matrix() plot color issues

pythonpandasmatplotlibdata-visualizationseaborn

提问by Francio Rodrigues

I am making a pd.scatter_matrix() plot from a DataFrame based on the Iris dataset colored by the target variable (plant species). 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).

我正在根据目标变量(植物物种)着色的 Iris 数据集从 DataFrame 制作 pd.scatter_matrix() 图。当我运行下面的代码时,我得到一个带有黑色、灰色和白色 (!) 彩色散射点的散射矩阵,这阻碍了可视化。网格似乎也不一致,显然只有靠近轴的图才能获得相应的网格。我想要一个遵循 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? How can I solve these visualization issues?

为什么 seaborn 绘图样式和 pd.scatter_matrix() 的使用强制执行不同的(可怕的!)调色板,然后是散点图和不一致网格线的默认值?如何解决这些可视化问题?

I already updated seaborn to a fairly recent version (0.8 of July 2017). Also tried the non-deprecated version the scatter_matrix plot for pandas pd.plotting.scatter_matrix() and had no luck. If I use the 'ggplot' style the color palette is correct for the scatter plots but the grids are still inconsistent.

我已经将 seaborn 更新到了一个相当新的版本(2017 年 7 月的 0.8)。还尝试了非弃用版本的 scatter_matrix plot for pandas pd.plotting.scatter_matrix() 并且没有运气。如果我使用 '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');

enter image description here

在此处输入图片说明

Package versions:

包版本:

pandas version: 0.20.1
matplotlib version: 2.0.2
seaborn version:0.8.0

Pandas版本:0.20.1
matplotlib 版本:2.0.2
seaborn 版本:0.8.0

采纳答案by Gayatri

I am not sure if this answers your question but you could use the 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:

这给了你:

enter image description here

在此处输入图片说明

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")

enter image description here

在此处输入图片说明

回答by Sergey Bushmanov

Default matplotlibsetting are not very aesthetic; however, do not underestimate the power of matplotlib.

默认matplotlib设置不是很美观;但是,不要低估matplotlib.

The simplestsolution 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);

enter image description here

在此处输入图片说明

(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 matplotlibrcfile. An example of what could be done with it could be found here

您可以根据需要调整matplotlibrc文件进一步自定义绘图。可以在此处找到可以使用它做什么的示例