I have the following code to produce seaborn scatterplots:
sns.set(
rc={"figure.figsize": (12.27, 4.27), "figure.dpi": 300, "savefig.dpi": 300},
font_scale=0.5,
style="white",
context="talk",
)
fig, ax = plt.subplots(1, 2)
sns.scatterplot(
data=df1,
x="PCA-1 (morgan)",
y="PCA-2 (morgan)",
hue="uncertainty",
palette="Set2_r",
ax=ax[0],
)
ax[0].set_xlabel("PCA-1 (Morgan)")
ax[0].set_ylabel("PCA-2 (Morgan)")
norm = plt.Normalize(0, df1["uncertainty"].max(),)
sm = plt.cm.ScalarMappable(cmap="Set2_r", norm=norm)
sm.set_array([])
# ax[0].get_legend().remove()
sns.scatterplot(
data=df2,
x="PCA-1 (morgan)",
y="PCA-2 (morgan)",
hue="uncertainty",
palette="Set2_r",
ax=ax[1],
)
ax[1].set_xlabel("PCA-1 (Morgan)")
ax[1].set_ylabel("PCA-2 (Morgan)")
# ax[1].get_legend().remove()
fig.colorbar(sm, ax=ax.flatten(), orientation="vertical")
The code above produces the following picture:
As you can see, the plot on the left has a colobar with a range (24, 48) for the hue I am using -- uncertainty
. I used the plt.Normalize
function to normalize from (0, 48). However, the plot on the right has a range (0.6, 1.6), but if you see it carefully, it is coloring the data points with the whole colormap instead of being constrained in the grey color area due to the range. How can I make the plot in the right color the points in grey because the range (0.6, 1.6) should be in that spectrum?
CodePudding user response:
You should explicitly specify vmin
and vmax
within your scatterplot. For your case,
vmin, vmax = df1.uncertainty.min(), df1.uncertainty.max()
sns.scatterplot(
data=df1,
...
ax=ax[0],
vmin = vmin,
vmax = vmax
)
sns.scatterplot(
data=df2,
...
ax=ax[1],
vmin = vmin,
vmax = vmax
)