数据标签中的散点图二进制数据颜色编码点

Scatter Plot Binary Data Color Coded Points from Data Labels

我想使用 matplotlib.pyplot.scatter 从数据框中的数据创建类似于下图的散点图,其中 header 的格式类似于此处的 table其中给定样本的所有点都根据数据第一列中的标签进行着色,并且仅为每个值为 1 的基因绘制一个点 - 值为 0 的基因没有点:

label gene a gene b gene c gene d
1 0 1 0 0
0 1 1 0 1
0 0 0 1 0
1 0 0 0 0
1 0 1 0 0

注意:我的示例数据与我的示例散点图输出不匹配。

将您的数据框融化为长格式后,您可以使用 seaborn 的 sns.relplot

绘制矩阵
import pandas as pd
import seaborn as sns
sns.set_style("ticks")

df = pd.read_html('
df['sample'] = df.index
df = df.melt(['label','sample'])

g = sns.relplot(
    data=df,
    x="variable", y="sample", hue="label", size="value",
    hue_norm=(-1, 1), palette='tab10',
    height=6, sizes=(10, 300), size_norm=(0, 1)
)
g.set(xlabel="Genes", ylabel="Samples",
    #   ylim=[df['sample'].max()+.5, df['sample'].min()-.5]  # uncomment to invert the y-axis
);

使用融化的数据框,您可以直接从 pandas 访问 plt.scatter,但我认为您必须为标签添加自己的自定义图例。

df.plot(x='variable', y='sample', s=(df.value+0.1) * 300, kind='scatter',
        ylim=[df['sample'].max()+.5, df['sample'].min()-.5], # uncomment to flip y-axis
        figsize=(7,6), c='label', cmap='coolwarm', colorbar=False
);