【发布时间】:2016-11-04 13:36:00
【问题描述】:
我想创建一个带有散点图颜色点的 seaborn 热图。我希望最终结果使用散点图的网格,热图的正方形在散点上“居中”。
不幸的是,我没有找到如何在两个层之间共享比例,如下例所示。
我能做什么?
非常感谢您的帮助。
%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
npoints = 3
x = np.tile(np.arange(npoints), npoints)
df = pd.DataFrame({'x': np.tile(np.arange(npoints), npoints), 'y': np.repeat(np.arange(npoints), npoints)})
df['z'] = 0
df.loc[df['x'] == df['y'], 'z'] = df.loc[df['x'] == df['y'], 'x']
df['c'] = np.random.choice(np.arange(3) + 1, df.shape[0])
df.loc[df['x'] != df['y'], 'c'] = 0
sns.heatmap(df[['x', 'y', 'z']].set_index(['x', 'y'])['z'].unstack())
plt.gca().set_title('Heatmap only')
df.plot(x='x', y='y', color=df['c'], kind='scatter')
plt.gca().set_title('Scatter points only')
fig, ax = plt.subplots()
sns.heatmap(df[['x', 'y', 'z']].set_index(['x', 'y'])['z'].unstack(), ax=ax)
df.plot(x='x', y='y', ax=ax, color=df['c'], kind='scatter')
ax.set_title('Heatmap and scatter points - scales problem')
【问题讨论】:
-
这个问题有点措辞不当,因为情节中并没有真正的 seaborn“层”,seaborn 只添加了 matplotlib 对象。您的问题不在于共享比例,而是在于意识到热图中的单元格跨越整数值(查看,例如`ax.get_xticks()`)。
标签: python pandas matplotlib seaborn