<p><code>FacetGrid</code><code>hue</code>是分类的,不是连续的。为<code>FacetGrid</code>中的散点图获取连续的颜色映射需要一点工作(与链接Github问题中的<code>imshow</code>不同,matplotlib不保留对“当前活动散点图映射器”的引用,因此对<code>plt.colorbar</code>的魔法调用不会获取应用于点颜色的映射)。</p>
<pre><code>g = sns.FacetGrid(iris, col='species', palette = 'seismic')
def facet_scatter(x, y, c, **kwargs):
"""Draw scatterplot with point colors from a faceted DataFrame columns."""
kwargs.pop("color")
plt.scatter(x, y, c=c, **kwargs)
vmin, vmax = 0, 7
cmap = sns.diverging_palette(240, 10, l=65, center="dark", as_cmap=True)
g = g.map(facet_scatter, 'sepal_length', 'sepal_width', "petal_length",
s=100, alpha=0.5, vmin=vmin, vmax=vmax, cmap=cmap)
# Make space for the colorbar
g.fig.subplots_adjust(right=.92)
# Define a new Axes where the colorbar will go
cax = g.fig.add_axes([.94, .25, .02, .6])
# Get a mappable object with the same colormap as the data
points = plt.scatter([], [], c=[], vmin=vmin, vmax=vmax, cmap=cmap)
# Draw the colorbar
g.fig.colorbar(points, cax=cax)
</code></pre>
<p><a href="https://i.stack.imgur.com/cTulb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cTulb.png" alt="enter image description here"/></a></p>