Getting legend in seaborn jointplot

2020-02-28 03:31发布

I'm interested in using the seaborn joint plot for visualizing correlation between two numpy arrays. I like the visual distinction that the kind='hex' parameter gives, but I would also like to know the actual count that different shades correspond to. Does anyone know how to put this legend on the side or even on the plot? I tried looking at the documentation and couldn't find it.

Thanks!

1条回答
冷血范
2楼-- · 2020-02-28 04:21

EDIT: updated to work with new Seaborn ver.

You need to do it manually by making a new axis with add_axes and then pass the name of the ax to plt.colorbar().

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

x = np.random.normal(0.0, 1.0, 1000)
y = np.random.normal(0.0, 1.0, 1000)
hexplot = sns.jointplot(x, y, kind="hex")
plt.subplots_adjust(left=0.2, right=0.8, top=0.8, bottom=0.2)  # shrink fig so cbar is visible
# make new ax object for the cbar
cbar_ax = hexplot.fig.add_axes([.85, .25, .05, .4])  # x, y, width, height
plt.colorbar(cax=cbar_ax)
plt.show()

Seaborn hexplot with colorbar

Sources: I almost gave up after I read a dev say that the

"work/benefit ratio [to implement colorbars] is too high"

but then I eventually found this solution in another issue.

查看更多
登录 后发表回答