问题描述
我正在绘制python热图.以下是我正在使用的代码.
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由以下形式的数据组成:
df consists of data of the form:
att1 att2 att3
fun1 1 2 1
fun2 1 0 0
.......
....
如果我使用了10-12之类的几行,此代码就可以正常工作.提供以下输出:
This code is working fine if I used few rows like 10-12. Giving following output:
但是,如果我增加等于800的行数,则该图看起来就像垃圾.以下是输出:
However if I increase number of rows equal to 800 the graph is looking like garbage. Following is the output:
移除plt.yticks后的图片:
Image after removing plt.yticks:
有人知道吗,如何增加这种热图的行数?
Does anyone have any idea, how can I increase number of rows in this kind of heatmap?
推荐答案
您可以使用aspect
选项,但是对于y-ticks,我不确定将它们全部都用是有用的,因为它不可读,但是您可以设置一些:
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])
希望这会有所帮助
这篇关于Python 2D热图中的行数增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!