Reputation: 5327
I am generating some professionally looking figures in seaborn.
For instance:
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)
Now, I would like to highlight part of the figure.
I would like the top histogram to have different colors for a certain range e.g. all bars where x<0.25 should be red, and corresponding point in the scatter plot as well.
How to obtain that?
I could repeat the plot operation using a subset of the original data, but then the histogram scaling would different.
Upvotes: 0
Views: 292
Reputation: 953
Note: This is not a complete answer
For scatter points you can annotate the points which are less than a given value:
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)
So if your dataframe looked like this:
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
Which should look like this:
Which should annotate the rows where x<=0.25
and the rest are not annotated. You can of course do more with annotation rather than annotating the index number.
With the histogram, I'm not sure how to do that, sorry :(. The seaborn doc doesn't help either.
Upvotes: 1