簡體   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