Sangeeta Sangeeta - 2 years ago 138
Python Question

Increasing Number of Rows in Python 2D Heatmap

I am plotting python heatmap. Following is the code I am using.

df = pd.read_csv('Book1.csv', index_col=0)
print (df)
# plotting
fig,ax = plt.subplots()
ax.matshow(df.mask(df.isin(df.att1)!=1), cmap=cm.Reds)
ax.matshow(df.mask(df.isin(df.att2)!=1), cmap=cm.Greens)
ax.matshow(df.mask(df.isin(df.att3)!=1), cmap=cm.Blues)
#ax.matshow(df.mask(df.isin(df.att4)!=1), cmap=cm.Reds)
#ax.matshow(df.mask(df.isin(df.att5)!=1), cmap=cm.Greens)
#ax.matshow(df.mask(df.isin(df.att6)!=1), cmap=cm.Blues)
plt.xticks(range(3), df.columns)
plt.yticks(range(864), df.index)

df consists of data of the form:

att1 att2 att3

fun1 1 2 1
fun2 1 0 0



This code is working fine if I used few rows like 10-12. Giving following output:enter image description here

However if I increase number of rows equal to 800 the graph is looking like garbage. Following is the output:enter image description here

Image after removing plt.yticks: enter image description here

Does anyone have any idea, how can I increase number of rows in this kind of heatmap?

Answer Source

You can use aspect option, but for the y-ticks, I'm not sure it's useful to have them all since it will be unreadable, but you can set some :

fig, ax = plt.subplots(figsize=(6, 10))

ax.matshow(df.mask(df.isin(df.att1)!=True), cmap=cm.Reds,  aspect='auto')
ax.matshow(df.mask(df.isin(df.att2)!=True), cmap=cm.Greens, aspect='auto')
ax.matshow(df.mask(df.isin(df.att3)!=True), cmap=cm.Blues, aspect='auto')

plt.xticks(range(3), df.columns)
plt.yticks(range(0, 800, 100), df.index[::100])

enter image description here

Hope this helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download