pandas 如何根据值为条带图中的单个点着色

qxsslcnc  于 2023-04-10  发布在  其他
关注(0)|答案(1)|浏览(141)

我试图根据定义的值为seaborn:stripplot上的特定点着色。例如,
value=(df['x']=0.0
我知道你可以使用regplot等来做到这一点:
df['color']= np.where( value==True , "#9b59b6")scatter_kws={'facecolors':df['color']}
有没有办法对pair gridstripplot进行着色?具体来说,在下面的t1t2中为指定值着色?
我也尝试过在hue中传递match var。然而,这产生了下面的图像#2,并且不是我要找的。
以下是df

par        t1         t2    found   
30000.0   0.50       0.45     yes   
10000.0   0.30       0.12     yes   
3000.0    0.40       0.00     no

下面是我的代码:

# Import dependencies
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("data.csv")

# Make the PairGrid
g = sns.PairGrid(df.sort_values("par", ascending=True),
                 x_vars=df.columns[1:3], y_vars=["par"], 
                 height=5, aspect=.65)

# Draw a dot plot using the stripplot function
g.map(sns.stripplot, size=16, orient="h", 
      linewidth=1, edgecolor="gray", palette="ch:2.5,-.2,dark=.3")

sns.set_style("darkgrid")

# Use the same x axis limits on all columns and add better labels
g.set(xlim=(-0.1, 1.1), xlabel="% AF", ylabel="")

# Use semantically meaningful titles for the columns
titles = ["Test 1", "Test 2"]

for ax, title in zip(g.axes.flat, titles):

    # Set a different title for each axes
    ax.set(title=title)

    # Make the grid horizontal instead of vertical
    ax.xaxis.grid(False)
    ax.yaxis.grid(True)

sns.despine(left=True, bottom=True)

我尝试用value=0.0为点着色不同的颜色:

match var传递到hue中会产生下面的结果,并删除第三个par=3000值并折叠图。我可以对我想用不同颜色突出显示的离群值进行分类。但是,离群值会从y轴中删除,图会折叠。

des4xlb0

des4xlb01#

你确定你想要一个stripplot吗?在我看来你实际上是想绘制一个scatterplot
另外,我认为你想使用FacetGrid而不是PairGrid?在这种情况下,它需要将你的 Dataframe 转换为“长格式”。
这是我得到的:

df2 = df.melt(id_vars=['par','found'], var_name='Test', value_name='AF')

g = sns.FacetGrid(data=df2, col='Test', col_order=['t1','t2'], hue='found',
                  height=5, aspect=.65)

g.map(sns.scatterplot, 'AF','par',
      s=100, linewidth=1, edgecolor="gray", palette="ch:2.5,-.2,dark=.3")

相关问题