Increasing Number of Rows in Python 2D Heatmap

拜拜、爱过 提交于 2020-01-05 12:10:50

问题


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)
plt.show()  

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:

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

Image after removing plt.yticks:

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


回答1:


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])

Hope this helps



来源:https://stackoverflow.com/questions/25228723/increasing-number-of-rows-in-python-2d-heatmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!