Python 海生热图上的散点图
我正在尝试在seaborn热图上标记一个位置(在本例中是沿着颜色贴图轴的最小值)。但我要标记的位置位于热图上的两个值之间,因为它是离散的,并且以比原始数据帧更低的分辨率绘制。我有一种感觉,我只是需要使用相同的轴来正确地过度绘制,但我似乎找不到任何有效的东西!相反,我的散点图要么总是出现在左下角,要么根本不出现 那么——我如何在这个低分辨率热图上绘制一个代表原始高分辨率数据帧中最小值的符号呢 这是我的密码:Python 海生热图上的散点图,python,pandas,heatmap,seaborn,Python,Pandas,Heatmap,Seaborn,我正在尝试在seaborn热图上标记一个位置(在本例中是沿着颜色贴图轴的最小值)。但我要标记的位置位于热图上的两个值之间,因为它是离散的,并且以比原始数据帧更低的分辨率绘制。我有一种感觉,我只是需要使用相同的轴来正确地过度绘制,但我似乎找不到任何有效的东西!相反,我的散点图要么总是出现在左下角,要么根本不出现 那么——我如何在这个低分辨率热图上绘制一个代表原始高分辨率数据帧中最小值的符号呢 这是我的密码: import matplotlib.pyplot as plt import seabor
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import math
number_of_planes = 100
cos_thetas = np.empty(number_of_planes)
phis = np.empty(number_of_planes)
for i in range(0,number_of_planes):
phi = np.random.uniform(0,2*math.pi)
theta = math.acos(2*np.random.uniform(0.5,1) - 1)
phis[i] = phi
cos_thetas[i] = math.cos(theta)
thicknesses = np.random.rand(number_of_planes, number_of_planes)
sns.set_style("darkgrid")
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
thick_df = pd.DataFrame(thicknesses*1000, columns=phis, index=cos_thetas)
#print thick_df
thick_df = thick_df.sort_index(axis=0, ascending=False)
thick_df = thick_df.sort_index(axis=1)
cmap = sns.cubehelix_palette(start=2.9, light=0.9, as_cmap=True, reverse=True)
yticks = np.linspace(0,1,6)
x_end = 6
xticks = np.arange(x_end+1)
m, n = 10, 10
row_groups = np.arange(len(thick_df.index)) // m
col_groups = np.arange(len(thick_df.columns)) // n
grpd = pd.DataFrame(thick_df.values, row_groups, col_groups)
val = pd.to_numeric(grpd.stack(), 'coerce').groupby(level=[0, 1]).mean().unstack().values
idx = thick_df.index.to_series().groupby(row_groups).mean().values
col = thick_df.columns.to_series().groupby(col_groups).mean().values
new_thick_df = pd.DataFrame(val, idx, col)
sns.heatmap(new_thick_df, linewidth=0, xticklabels=xticks, yticklabels=yticks[::-1], square=True, cmap=cmap, ax=ax)
#new_thick_df.plot.scatter(thick_df.columns.argmin(), thick_df.index.argmin(), ax=ax, c='r', s=100)
#One problem here is that thick_df.columns.argmin() gives an integer position instead of the column label
ax.scatter(thick_df.columns.argmin(), thick_df.index.argmin(), marker='*', s=100, color='yellow')
ax.set_xticks(xticks*ax.get_xlim()[1]/(2*math.pi))
ax.set_yticks(yticks*ax.get_ylim()[1])
ax.set_xlabel(r'$\rm{\phi}$', fontsize=16)
ax.set_ylabel(r'$\rm{\cos\ \theta}$', fontsize=16)
plt.figtext(0.865, 0.5, r'$\rm{thickness\ (kpc)}$', fontsize=15, rotation=270, horizontalalignment='left', verticalalignment='center')
plt.show()
从注释中,您只需要从绝对最小值
thick\u df
绘制一个星形。您已将每10x10个厚度_df
段平均化为新厚度_df
。热图是根据new\u-thick\u-df
创建的,但您希望在此热图上绘制最小值fromthick\u-df
。您必须首先通过展平厚_df
找到最小值,然后减小尺寸,以便将其转换为0到10之间的范围。我还为新的\u-thick\u-df
的最小值绘制了一颗红星
将此行更改为:ax.scatter(thick_-df.columns.argmin()、thick_-df.index.argmin()、marker='*',s=100,color='yellow')
idx_min_big = thick_df.values.flatten().argmin()
x_min_big, y_min_big = (idx_min_big % 100) / 10 , 10 - (idx_min_big // 100) / 10
ax.scatter(x_min_big, y_min_big, marker='*', s=100, color='yellow')
# get min of new_thick_df
min_idx = new_thick_df.values.flatten().argmin()
x_min, y_min = min_idx % 10 + .5, 9 - min_idx // 10 + .5
ax.scatter(x_min, y_min, marker='*', s=100, color='yellow')
为了证明thick_df
的最小值与这个逻辑是一致的
x,y = idx_min_big // 100, idx_min_big % 100
thick_df.iloc[x, y]
输出
0.075901121550980832
0.075901121550980832
最低限度
thick_df.values.flatten().min()
输出
0.075901121550980832
0.075901121550980832
由于您对粗索引进行了排序,因此
thick\u df.columns.argmin()和thick\u df.index.argmin()
将始终为0和99。对于两个轴,绘图的x/y范围为0-10。你的意思是在散点图中画星星时使用new\u-thick\u-df
?这仍然会产生0,9,但我不想知道cosθ和φ在哪里最小,我想知道厚度在哪里最小。所以我需要画一个cos_θ和φ值,对应于数据帧中最小的元素。实际上我指的是粗_df而不是上面的新_粗_df,因为我想要在低分辨率数据帧上绘制原始数据帧的高分辨率最小值。好的,但是排序没有意义。它将始终产生0,99。摆脱排序,只需除以10。请参阅更新的答案或更一般的答案,以绘制位于我的热图上的正方形值之间的任何特定值。不,排序是有意义的,因为这是使φ和cos_θ的轴标签正确的原因。我不想得到φ和cos_θ的最小值,所以没有理由它总是0,99。我实际上不认为argmin是这个函数的正确函数,但我找不到一个这样的函数。