从 DataFrame 图制作 seaborn 热图以了解数据范围
making seaborn heatmap from DataFrame plot to be aware of data ranges
如何使 seaborn 热图(从 pandas DataFrame 图创建)了解数据范围? IE。当我将鼠标指针悬停在图上时,我可以在图的右下角看到 window "x= y=",而我想看到坐标我悬停在图上的点(例如,"x=25.6, y=3.3"),当然假设输入 DataFrame 包含一个 2D 直方图,每个轴上的 bin 大小相等。
或者,也许我可以用不同的方式创建这样的情节来达到同样的效果?例如,使用 ax.hist2d
我开箱即用,但我希望能够使用每个 bin 的自定义代码内容进行计算,并使其有效地成为一个热图图(使用 bin 内容的颜色编码)。
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Index = [ 1.0, 2.0, 3.0, 4.0, 5.0]
Cols = [10.0, 20.0, 30.0, 40.0, 50.0]
df = pd.DataFrame(abs(np.random.randn(5, 5)),
index=Index, columns=Cols)
plt.close(1)
fig,ax = plt.subplots(num=1)
sns.heatmap(df, annot=True)
plt.show(block=False)
感谢您的帮助!
如果您将 sns.heatmap(...)
替换为 ax.imshow(..)
,您就接近您的需要了。然后您可以将图像的范围设置为您需要的数据范围。
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
Index = [ 1.0, 2.0, 3.0, 4.0, 5.0]
Cols = [10.0, 20.0, 30.0, 40.0, 50.0]
df = pd.DataFrame(abs(np.random.randn(5, 5)),
index=Index, columns=Cols)
plt.close(1)
fig,ax = plt.subplots(num=1)
dx = np.diff(df.columns)[0]/2
dy = np.diff(df.index)[0]/2
extent = [df.columns.min()-dx, df.columns.max()+dx,
df.index.min()-dy, df.index.max()+dy]
ax.imshow(df, extent=extent, aspect="auto")
plt.show()
from matplotlib.ticker import FixedFormatter
class CustomFormatter(FixedFormatter):
def __init__(self, old):
super().__init__(old.seq)
def __call__(self, x, pos=None):
return self.seq[abs(self.locs - x).argmin()]
plt.gca().xaxis.set_major_formatter(CustomFormatter(plt.gca().xaxis.get_major_formatter()))
plt.gca().yaxis.set_major_formatter(CustomFormatter(plt.gca().yaxis.get_major_formatter()))
如何使 seaborn 热图(从 pandas DataFrame 图创建)了解数据范围? IE。当我将鼠标指针悬停在图上时,我可以在图的右下角看到 window "x= y=",而我想看到坐标我悬停在图上的点(例如,"x=25.6, y=3.3"),当然假设输入 DataFrame 包含一个 2D 直方图,每个轴上的 bin 大小相等。
或者,也许我可以用不同的方式创建这样的情节来达到同样的效果?例如,使用 ax.hist2d
我开箱即用,但我希望能够使用每个 bin 的自定义代码内容进行计算,并使其有效地成为一个热图图(使用 bin 内容的颜色编码)。
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Index = [ 1.0, 2.0, 3.0, 4.0, 5.0]
Cols = [10.0, 20.0, 30.0, 40.0, 50.0]
df = pd.DataFrame(abs(np.random.randn(5, 5)),
index=Index, columns=Cols)
plt.close(1)
fig,ax = plt.subplots(num=1)
sns.heatmap(df, annot=True)
plt.show(block=False)
感谢您的帮助!
如果您将 sns.heatmap(...)
替换为 ax.imshow(..)
,您就接近您的需要了。然后您可以将图像的范围设置为您需要的数据范围。
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
Index = [ 1.0, 2.0, 3.0, 4.0, 5.0]
Cols = [10.0, 20.0, 30.0, 40.0, 50.0]
df = pd.DataFrame(abs(np.random.randn(5, 5)),
index=Index, columns=Cols)
plt.close(1)
fig,ax = plt.subplots(num=1)
dx = np.diff(df.columns)[0]/2
dy = np.diff(df.index)[0]/2
extent = [df.columns.min()-dx, df.columns.max()+dx,
df.index.min()-dy, df.index.max()+dy]
ax.imshow(df, extent=extent, aspect="auto")
plt.show()
from matplotlib.ticker import FixedFormatter
class CustomFormatter(FixedFormatter):
def __init__(self, old):
super().__init__(old.seq)
def __call__(self, x, pos=None):
return self.seq[abs(self.locs - x).argmin()]
plt.gca().xaxis.set_major_formatter(CustomFormatter(plt.gca().xaxis.get_major_formatter()))
plt.gca().yaxis.set_major_formatter(CustomFormatter(plt.gca().yaxis.get_major_formatter()))