在 Seaborn 热图中保留一些刻度
Keeping some ticks in Seaborn heatmap plot
我有这个数据框:
Position FLK
75.38 1.653608512
75.45 2.404366644
75.47 4.281285701
75.48 5.457803236
75.56 4.281285701
75.61 3.348777004
75.63 5.457803236
75.67 4.281285701
75.70 2.863168526
75.74 2.404366644
75.75 2.863168526
75.78 2.863168526
75.93 3.348777004
75.96 2.863168526
75.99 2.863168526
76.07 0.167253206
76.11 2.863168526
76.14 2.863168526
我使用这个脚本在 Seaborn 中绘制热图:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
df = pd.read_csv("D.txt",delimiter=r"\s+",header=0)
df.set_index('Position')
FLK=(df[['FLK']]).T
sns.heatmap(FLK)
plt.show()
然后得到这个数字:
我想知道如何在此图中保留一些 x 刻度 (0,2,3,4,5,6,12,15),如下所示:
您可以使用 plt.ticks
到 select 某些报价位置。创建此热图的方式会在位置 0.5, 1.5, 2.5, ....
处生成 x 个带有名称 '0', '1', '2', ...
的文本标签的刻度。因此,您可以通过 plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
.
select 一些价格变动
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({'FLK': np.random.uniform(0, 5, 18)})
FLK = (df[['FLK']]).T
sns.heatmap(FLK)
plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
plt.tight_layout()
plt.show()
PS:请注意,df.set_index('Position')
要么需要重新赋值给df
,要么需要参数inplace=True
才能发挥作用。在这种情况下,标签将是这些索引,您可以 select 具有相同 plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
.
的子集
我有这个数据框:
Position FLK
75.38 1.653608512
75.45 2.404366644
75.47 4.281285701
75.48 5.457803236
75.56 4.281285701
75.61 3.348777004
75.63 5.457803236
75.67 4.281285701
75.70 2.863168526
75.74 2.404366644
75.75 2.863168526
75.78 2.863168526
75.93 3.348777004
75.96 2.863168526
75.99 2.863168526
76.07 0.167253206
76.11 2.863168526
76.14 2.863168526
我使用这个脚本在 Seaborn 中绘制热图:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
df = pd.read_csv("D.txt",delimiter=r"\s+",header=0)
df.set_index('Position')
FLK=(df[['FLK']]).T
sns.heatmap(FLK)
plt.show()
然后得到这个数字:
我想知道如何在此图中保留一些 x 刻度 (0,2,3,4,5,6,12,15),如下所示:
您可以使用 plt.ticks
到 select 某些报价位置。创建此热图的方式会在位置 0.5, 1.5, 2.5, ....
处生成 x 个带有名称 '0', '1', '2', ...
的文本标签的刻度。因此,您可以通过 plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({'FLK': np.random.uniform(0, 5, 18)})
FLK = (df[['FLK']]).T
sns.heatmap(FLK)
plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
plt.tight_layout()
plt.show()
PS:请注意,df.set_index('Position')
要么需要重新赋值给df
,要么需要参数inplace=True
才能发挥作用。在这种情况下,标签将是这些索引,您可以 select 具有相同 plt.xticks(np.array([0, 2, 3, 4, 5, 6, 12, 15]) + 0.5)
.