Reputation: 3907
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.
My problem is with the axis. As can be seen in the picture below, the resulting axis are strings, that stand for bin boundaries. 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()
Upvotes: 2
Views: 9908
Reputation: 339725
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()
Upvotes: 2