繁体   English   中英

Seaborn热图与数字轴

[英]Seaborn heatmap with numerical axes

我想用第二个图表覆盖热图(KDE图,但是对于这个例子,我将使用散点图,因为它显示了相同的问题)。

Seaborn热图具有分类轴,因此使用数字轴覆盖图表不会正确排列两个图表。

例:

df = pd.DataFrame({2:[1,2,3],4:[1,3,5],6:[2,4,6]}, index=[3,6,9])
df

    2   4   6
3   1   1   2
6   2   3   4
9   3   5   6

fig, ax1 = plt.subplots(1,1)
sb.heatmap(df, ax=ax1, alpha=0.1)

在此输入图像描述

用散点图覆盖它:

fig, ax1 = plt.subplots(1,1)
sb.heatmap(df, ax=ax1, alpha=0.1)
ax1.scatter(x=5,y=5, s=100)
ax1.set_xlim(0,10)
ax1.set_ylim(0,10)

有没有办法说服热图使用列和索引值作为数值?

在此输入图像描述

你不能“说服” heatmap不要产生分类情节。 最好使用另一个使用数字轴的图像图。 例如,使用pcolormesh图。 当然,假设列和行是均匀分布的。 然后,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({2:[1,2,3],4:[1,3,5],6:[2,4,6]}, index=[3,6,9])

c = np.array(df.columns)
x = np.concatenate((c,[c[-1]+np.diff(c)[-1]]))-np.diff(c)[-1]/2.
r = np.array(df.index)
y = np.concatenate((r,[r[-1]+np.diff(r)[-1]]))-np.diff(r)[-1]/2.
X,Y = np.meshgrid(x,y)


fig, ax = plt.subplots(1,1)
pc = ax.pcolormesh(X,Y,df.values, alpha=0.5, cmap="magma")
fig.colorbar(pc)
ax.scatter(x=5,y=5, s=100)
ax.set_xlim(0,10)
ax.set_ylim(0,10)

plt.show()

产生

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM