[英]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])
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
region
和event
都按字母顺序绘制,这就是使用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.