我想创建一个海洋热图,其中还散布了色点。我希望最终结果使用散点图的网格,而热图的正方形位于散点的“中心”。

不幸的是,我找不到如何在两层之间共享比例,如下面的示例所示。

我能做什么?

非常感谢你的帮助。

%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')


python - Python:在matplotlib和seaborn之间共享规模-LMLPHP
python - Python:在matplotlib和seaborn之间共享规模-LMLPHP
python - Python:在matplotlib和seaborn之间共享规模-LMLPHP

最佳答案

一种解决方法是将分散数据移动0.5

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

fig, ax = plt.subplots()
qp = 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.scatter(df['x']+0.5,df['y']+0.5,c=df['c'])

ax.set_title('Heatmap and scatter points - scales problem')

plt.show()


结果:

python - Python:在matplotlib和seaborn之间共享规模-LMLPHP

关于python - Python:在matplotlib和seaborn之间共享规模,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40424058/

10-10 18:37