我希望边缘图仅显示图例中显示的 6 种颜色和值范围。现在,它显示了更多内容,并且在绘图中看起来很混乱,无法确定一条线跟踪的位置。 我使用 Seaborn jointplot 绘制边缘彩色直方图。
这是我的 python 代码,用于一些 pandas 数据框(df):
mask = df['rf_number'].notnull()
%matplotlib inline
plt.figure(figsize=(10,10))
#sns.scatterplot(x=df['x'][mask], y=df['y'][mask],color='black', linewidth=0)
colored_hist = sns.jointplot(data= df[mask], x='x', y= 'y', hue='rf_number',
kind='scatter', palette='Spectral', linewidth=0,
size=12, xlim=[-11,11], ylim=[-10,10],
marginal_kws=dict(fill=False)
)
任何帮助表示赞赏!
通过将“rf_number”四舍五入为
0.2
的倍数,kdeplot 将仅考虑这些四舍五入的值。
请注意,您需要删除
plt.figure(figsize=(10,10))
,因为 sns.jointplot()
创建自己的图形。 sns.jointplot(..., height=10)
会使无花果大小10x10
。
import seaborn as sns
import numpy as np
import pandas as pd
# create some random test data
df = pd.DataFrame({'x': np.random.randn(1000) * 3, 'y': np.random.randn(1000) * 3})
df['rf_number'] = ((np.sin(df['x'] * np.cos(df['y'] / 7) + 4) + np.sin(df['y'] / 3 + 1)) ** 2) / 4
# round the hue column to multiples of 0.2
df['rf_rounded'] = (df['rf_number'] * 5).round() / 5
mask = df['rf_rounded'].notnull()
colored_hist = sns.jointplot(data=df[mask], x='x', y='y', hue='rf_rounded',
kind='scatter', palette='Spectral', linewidth=0,
size=12, xlim=[-11, 11], ylim=[-10, 10],
marginal_kws=dict(fill=False),
height=10)
sns.move_legend(colored_hist.ax_joint, loc='best', title='DEE probability')