我试图将颜色条添加到3个seaborn图的网格中。我可以将颜色条添加到3个单独的图中,或者挤压第3个图旁边的颜色条。我希望在第3个绘图之后有一个颜色条,而不会改变最后一个绘图的大小。
我从这个答案中得到了很多好主意,但无法解决我的确切问题:SO Question/Answer
这是我目前的代码:
import seaborn as sns
def masked_vs_unmasked_facets(output_dir, merged_df, target_col, thresholds):
# defining the maximal values, to make the plot square
z_min = merged_df[['z_full', 'z_masked']].min(axis=0, skipna=True).min(skipna=True)
z_max = merged_df[['z_full', 'z_masked']].max(axis=0, skipna=True).max(skipna=True)
z_range_value = max(abs(z_min), abs(z_max))
# Setting the column values to create the facet grid
for i, val in enumerate(thresholds):
merged_df.loc[merged_df.info_score_masked > val, 'PlotSet'] = i
# Start the actual plots
g = sns.FacetGrid(merged_df, col='PlotSet', size=8)
def facet_scatter(x, y, c, **kwargs):
kwargs.pop("color")
plt.scatter(x, y, c=c, **kwargs)
# plt.colorbar() for multiple colourbars
vmin, vmax = 0, 1
norm=plt.Normalize(vmin=vmin, vmax=vmax)
g = (g.map(facet_scatter, 'z_full', 'z_masked', 'info_score_masked', norm=norm, cmap='viridis'))
ax = g.axes[0]
for ax in ax:
ax.set_xlim([-z_range_value * 1.1, z_range_value * 1.1])
ax.set_ylim([-z_range_value * 1.1, z_range_value * 1.1])
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3")
plt.colorbar() # Single squashed colorbar
plt.show()
masked_vs_unmasked_facets(output_dir, masking_results, 'info_score_masked', [0, 0.7, 0.9])
答案 0 :(得分:1)
根据@ImportanceOfBeingEarnest的建议,我发现我需要在facetgrid中添加另一组轴,然后将这些轴分配给颜色条。为了将这个额外的元素保存到图中,我使用bbox_extra_artist
kwarg作为紧密边界框。另一个小的补充是一个小句子来捕捉我的一个方面没有数据的边缘情况。在这种情况下,我在一个空行中添加了一个类别的实例,因此每个类别总共至少有一行。
import seaborn as sns
def masked_vs_unmasked_facets(output_dir, merged_df, target_col, thresholds):
z_min = merged_df[['z_full', 'z_masked']].min(axis=0, skipna=True).min(skipna=True)
z_max = merged_df[['z_full', 'z_masked']].max(axis=0, skipna=True).max(skipna=True)
z_range_value = max(abs(z_min), abs(z_max))
for i, val in enumerate(thresholds):
merged_df.loc[merged_df.info_score_masked > val, 'PlotSet'] = i
# Catch instances where there are no values in category, to ensure all facets are drawn each time
if i not in merged_df['PlotSet'].unique():
dummy_row = pd.DataFrame(columns=merged_df.columns, data={'PlotSet': [i]})
merged_df = merged_df.append(dummy_row)
g = sns.FacetGrid(merged_df, col='PlotSet', size=8)
def facet_scatter(x, y, c, **kwargs):
kwargs.pop("color")
plt.scatter(x, y, c=c, **kwargs)
vmin, vmax = 0, 1
norm=plt.Normalize(vmin=vmin, vmax=vmax)
g = (g.map(facet_scatter, 'z_full', 'z_masked', 'info_score_masked', norm=norm, cmap='viridis'))
titles = ["Correlation for all masked / unmasked z-score with {} above {}".format(target_col, threshold) for threshold in thresholds]
axs = g.axes.flatten()
for i, ax in enumerate(axs):
ax.set_title(titles[i])
ax.set_xlim([-z_range_value * 1.1, z_range_value * 1.1])
ax.set_ylim([-z_range_value * 1.1, z_range_value * 1.1])
ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3")
cbar_ax = g.fig.add_axes([1.015,0.13, 0.015, 0.8])
plt.colorbar(cax=cbar_ax)
# extra_artists used here
plt.savefig(os.path.join(output_dir, 'masked_vs_unmasked_scatter_final.png'), bbox_extra_artists=(cbar_ax,), bbox_inches='tight')
masked_vs_unmasked_facets(output_dir, masking_results, 'info_score_masked', [0, 0.7, 0.9])
这给了我: