简体   繁体   English

seaborn.pairplot() 改变每个图形的颜色

[英]seaborn.pairplot() changing the color of each graph

I am trying to produce a simple pairplot with each graph with a separate color.我正在尝试使用不同颜色的每个图形生成一个简单的pairplot图。 I don't know if this is possible as I am not using hue .我不知道这是否可行,因为我没有使用hue

My dataset is as such:我的数据集是这样的:

      High Jump  Discus Throw  Long Jump
0           859           732       1061
1           749           823        975
2           887           778        866
3           878           790        898
4           803           789        913
     ...           ...        ...
7963        714           571        760
7964        767           573        845
7965        840           461        804
7966        758           487        720
7967        714           527        809

My code and graph looks as such:我的代码和图表如下所示:

t = sns.pairplot(new)

在此处输入图片说明

Is there any way to make this more colourful?有什么办法可以让这个颜色更鲜艳吗?

Since PairGrid automatically passes a color attribute to the plotting function, one way to get a different color per plot is to create your own plotting function that ignores the color passed by PairGrid (note that you loose the possibility to color code by hues obviously)由于PairGrid自动将颜色属性传递给绘图函数,因此每个绘图获得不同颜色的一种方法是创建自己的绘图函数,该函数忽略PairGrid传递的颜色(请注意,您显然无法通过hues对颜色进行编码)

colors = iter(['xkcd:red purple', 'xkcd:pale teal', 'xkcd:warm purple',
       'xkcd:light forest green', 'xkcd:blue with a hint of purple',
       'xkcd:light peach', 'xkcd:dusky purple', 'xkcd:pale mauve',
       'xkcd:bright sky blue', 'xkcd:baby poop green', 'xkcd:brownish',
       'xkcd:moss green', 'xkcd:deep blue', 'xkcd:melon',
       'xkcd:faded green', 'xkcd:cyan', 'xkcd:brown green',
       'xkcd:purple blue', 'xkcd:baby shit green', 'xkcd:greyish blue'])

def my_scatter(x,y, **kwargs):
    kwargs['color'] = next(colors)
    plt.scatter(x,y, **kwargs)

def my_hist(x, **kwargs):
    kwargs['color'] = next(colors)
    plt.hist(x, **kwargs)

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map_diag(my_hist)
g.map_offdiag(my_scatter)

在此处输入图片说明

Since you don't have any categorical data like gender, you can use PairGrid to manipulate upper, lower or diagonal graphs in a grid to make it more colorful.由于您没有任何诸如性别之类的分类数据,您可以使用 PairGrid 来操作网格中的上、下或对角图形,使其更加丰富多彩。

import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('dataset.csv')

g = sns.PairGrid(df)
g.map_upper(sns.scatterplot,color='red')
g.map_lower(sns.scatterplot, color='green')
g.map_diag(plt.hist)

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

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