繁体   English   中英

如何在 matplotlib 模块中应用 seaborn.scatterplot(style)?

[英]How can I apply seaborn.scatterplot(style) in matplotlib module?

我试图让这个图只使用 matplotlib 模块。 我可以制作 x、y 图例,但我不知道如何在 matplotlib 模块中应用 seaborn.scatterplot(style)。 任何人都可以帮助我如何制作这个情节?

下图代码是这样的:

import matplotlib.pyplot as plt
import seaborn as sns

fmri = sns.load_dataset('fmri')

fmri.head()

sns.scatterplot(x = 'timepoint', y = 'signal', hue = 'region', style = 'event', data = fmri)

在此处输入图片说明

这就是我正在尝试编写的代码

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches 

fig, ax = plt.subplots()

colors = {'parietal' : 'tab:blue', 'frontal' : 'orange'}

scatter = ax.scatter(x = fmri['timepoint'],y = fmri['signal'],c = fmri['region'].apply(lambda x: colors[x]),s = 15)

parietal = mpatches.Patch(color = 'tab:blue',label = 'parietal')

frontal = mpatches.Patch(color = 'orange',
                         label = 'frontal')

plt.xlabel('timepoint')

plt.ylabel('signal')

plt.legend(handles = [parietal, frontal])

在此处输入图片说明

重现 Seaborn 的情节

  • 将每个特征分成一个数据框,并用选择的标记和颜色绘制该数据框
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# load the data set
fmri = sns.load_dataset('fmri')

# create separate dataframe for each group of data
fc = fmri[(fmri.region == 'frontal') & (fmri.event == 'cue')]
fs = fmri[(fmri.region == 'frontal') & (fmri.event == 'stim')]
pc = fmri[(fmri.region == 'parietal') & (fmri.event == 'cue')]
ps = fmri[(fmri.region == 'parietal') & (fmri.event == 'stim')]

# create a list with the data, color, marker and label
dfl = [(ps, 'C0', 'o', 'Parietal: Stim'), (pc, 'C0', 'x', 'Parietal: Cue'),
       (fs, 'C1', 'o', 'Frontal: Stim'), (fc, 'C1', 'x', 'Frontal: Cue')]

# plot
plt.figure(figsize=(10, 7))
for data, color, marker, label in dfl:
    plt.scatter('timepoint', 'signal', data=data, color=color, marker=marker, label=label)

plt.legend(title='Region: Event')
plt.xlabel('timepoint')
plt.ylabel('signal')
plt.show()

在此处输入图片说明

来自groupby绘图

  • pandas.DataFrame.groupby'region' ,然后绘图。
  • 这可能是最简单的方法,没有seaborn
    • 最简单的是不需要手动创建每个数据子集。
  • 每个regionevent都按字母顺序绘制,这就是使用cmap指定颜色的原因。
  • 由于blue (C0) 是第二个(顶部)绘制的,它看起来像是主色。
  • 我添加了s (大小)和alpha ,可以根据需要删除或更改它们。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# load the data set
fmri = sns.load_dataset('fmri')

# map for color and marker
pmap = {'parietal_cue': ['C0', 'x'], 'parietal_stim': ['C0', 'o'], 'frontal_cue': ['C1', 'x'], 'frontal_stim': ['C1', 'o']}

# Groupby and plot
plt.figure(figsize=(10, 7))
for g, df in fmri.groupby(['region', 'event']):
    
    # get values from dict for group g
    maps = pmap[f'{g[0]}_{g[1]}']
    
    plt.scatter('timepoint', 'signal', data=df, c=maps[0], marker=maps[1], s=15, alpha=0.5, label=f'{g[0]}: {g[1]}')

plt.legend(title='Region: Event')
plt.xlabel('timepoint')
plt.ylabel('signal')
plt.show()

在此处输入图片说明

使用seaborn

  • 没有意义,不使用seaborn ,因为seaborn只是matplotlib的高级 API。
  • 从配置的角度来看,您想要使用matplotlib做的任何事情,也可以使用相同或类似的方法对seaborn图形完成。
    • 例如为legend创建自定义Patch
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Patch

plt.figure(figsize=(10, 7))
p = sns.scatterplot(x='timepoint', y='signal', hue='region', data=fmri)

# get legend handle and labels
h, l = p.get_legend_handles_labels()

# create a new patch
patches = [Patch(color=k.get_fc()[0], label=v) for k, v in list(zip(h, l))]

# add the legend
plt.legend(handles=patches)

在此处输入图片说明

使用seaborn.stripplot

  • 由于有太多重叠的数据,我认为最好的绘图选项,在这种情况下,是seaborn.stripplot
plt.figure(figsize=(12, 7))
sns.stripplot(x='timepoint', y='signal', hue='region', s=4, alpha=0.6, jitter=True, data=fmri)

在此处输入图片说明

我不确定您为什么要使用 matplotlib 重现它,但我使用 seaborn 的数据在 matplotlib 中绘制了两个参数的图形。 我需要使用相同的技术添加其他两个参数。

import matplotlib.pyplot as plt
import seaborn as sns

fmri = sns.load_dataset('fmri')

plt.style.use('seaborn-notebook')

fig, ax = plt.subplots()

ax.scatter(x = fmri.loc[fmri['region'] == 'parietal',
                        ['timepoint']], y = fmri.loc[fmri['region'] == 'parietal',['signal']],
                        s = 15, label='parietal', marker='o')
ax.scatter(x = fmri.loc[fmri['region'] == 'parietal', 
                        ['timepoint']], y = fmri.loc[fmri['region'] == 'frontal',['signal']],
                        s = 15, label='frontal', marker='o')

plt.xlabel('timepoint')
plt.ylabel('signal')

ax.legend(title='region')

plt.show()

在此处输入图片说明

暂无
暂无

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

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