简体   繁体   English

更改海洋热图的轴

[英]Changing the axis of a seaborn heatmap

I have a set of Cartesian coordinates pairs, along with a binary variable for each of the pairs. 我有一组笛卡尔坐标对,以及每个对的二进制变量。 I am plotting a heatmap, where in each bin, I compute the fraction of coordinates falling into this bin where the binary variable is 1. 我正在绘制一个热图,在每个图元中,我计算落入该图元中二进制变量为1的坐标的比例。

My problem is with the axis. 我的问题是轴。 As can be seen in the picture below, the resulting axis are strings, that stand for bin boundaries. 如下图所示,结果轴是字符串,代表bin边界。 I would like the axis to be Cartesian coordinates. 我希望轴为笛卡尔坐标。 Is there a simple way to change this? 有没有简单的方法可以更改此设置?

import numpy as np
import pandas as pd
import seaborn as sb
np.random.seed(0)
x = np.random.uniform(0,100, size=200)
y = np.random.uniform(0,100, size=200)
z = np.random.choice([True, False], size=200, p=[0.3, 0.7])
df = pd.DataFrame({"x" : x, "y" : y, "z":z})
binsx = 8
binsy = 5
res = df.groupby([pd.cut(df.y, binsy),pd.cut(df.x,binsx)])['z'].mean().unstack()
ax = sb.heatmap(res)
ax.axis('equal')
ax.invert_yaxis()

在此处输入图片说明

The following creates a scale by using the bins for histogramming as the extents of the image. 下面通过使用用于直方图的箱作为图像范围来创建比例。

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

np.random.seed(0)
x = np.random.uniform(0,100, size=200)
y = np.random.uniform(0,100, size=200)
z = np.random.choice([True, False], size=200, p=[0.3, 0.7])
df = pd.DataFrame({"x" : x, "y" : y, "z":z})
binsx = np.arange(0,112.5,12.5)
binsy = np.arange(0,120,20)
res = df.groupby([pd.cut(df.y, binsy),pd.cut(df.x,binsx)])['z'].mean().unstack()

plt.imshow(res, cmap=plt.cm.Reds, 
           extent=[binsx.min(), binsx.max(),binsy.min(),binsy.max()])
plt.xticks(binsx)
plt.yticks(binsy)
plt.colorbar()
plt.grid(False)


plt.show()

在此处输入图片说明

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

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