我正在为Seaborn培养一些专业的人物。
例如:
import seaborn as sns
sns.set_style('white')
sns.set_context('paper', font_scale=2)
sns.set_style('ticks', {'axes.edgecolor': '0',
'xtick.color': '0',
'ytick.color': '0'})
g = sns.jointplot(x=np.random.rand(1000,3), y=np.random.rand(1000,3),
marginal_kws=dict(bins=50, rug=True,color="k"),
annot_kws=dict(stat="r"),
s=40, edgecolor="k",color='w', linewidth=1)
sns.despine(offset=5, trim=True)
g.fig.set_figheight(12)
g.fig.set_figwidth(12)
现在,我要强调图中的一部分。
我希望顶部直方图在一定范围内具有不同的颜色 例如x <0.25的所有条形应为红色,散点图中的相应点也应为红色。
如何获取?
我可以使用原始数据的子集来重复绘图操作,但是直方图的缩放比例会有所不同。
答案 0 :(得分:1)
注意:这不是完整的答案
对于散点,您可以注释小于给定值的点:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame(columns=("x", "y", "x_cor_to_ann"))
df['x'] = pd.Series(np.random.randn(10))
df['y'] = pd.Series(np.random.randn(10))
df['x_cor_to_ann'] = df['x'] <= 0.25
print(df)
g = sns.jointplot("x", "y", data=df, kind="reg",
marginal_kws=dict(bins=50, rug=True,color="k"),
annot_kws=dict(stat="r"),
color='b')
highlight = df[df['x']<=0.25]
def annotate(data_row):
r = data_row[1]
plt.gca().annotate(data_row[0], xy=(r["x"], r["y"]),
xytext=(2,2) , textcoords ="offset points")
for data_row in highlight.iterrows():
annotate(data_row)
plt.show()
sns.despine(offset=5, trim=True)
g.fig.set_figheight(12)
g.fig.set_figwidth(12)
因此,如果您的数据框看起来像这样:
x y x_cor_to_ann
0 1.180725 0.368682 False
1 1.816001 0.104237 False
2 1.074462 1.253019 False
3 -0.219031 -0.670320 True
4 0.402527 -0.899265 False
5 -0.960773 2.942559 True
6 0.067588 0.508284 True
7 -0.051982 -0.194170 True
8 -0.016429 0.101746 True
9 1.607429 0.736233 False
应如下所示:
哪个应该注释x<=0.25
其余未注释的行。当然,您可以通过注释而不是注释索引号来做更多的事情。
对于直方图,我不确定该怎么做,很抱歉:(。原始文档也无济于事。