更改plt.imshow中第一个y轴的刻度之间的间隔

时间:2018-12-06 08:32:47

标签: python matplotlib plot axes imshow

嘿,我使用了以下代码:

import matplotlib.pyplot as plt
import numpy as np

Class1=np.array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [1., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 0., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 1., 1., 0., 1., 0., 1., 0., 1.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 1., 1., 1., 0.]])

Unique=np.array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
   13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
   26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38.])
counts=np.array([56184982,  2904400,  4950837,   114746,     4270,   182052,
     844444,   106292,    47007,    68480,    23522,    13838,
      23990,   301704,    50462,    21345,    12263,     7217,
      64194,      251,      885,  2947045,  4895643,   102992,
       4401,   180136,   844872,      281,   107200,    48703,
      69791,    22651,    50295,    22772,     7020,    62543,
         43,     1103,       48])

list_y2=[]
list_x=[]
list_y1=[]
for i in np.arange(len(Unique)):
  list_y2.append( np.array2string(round(counts[i]/Class1.sum(axis=1)[i],2)))
  if i < 9:
    list_x.append(str(i+1))    
  else:
    list_x.append( str(i+1))
  list_y1.append( str(38-int(Unique[i])))

fig, ax1 = plt.subplots()
F=16
ax1.set_xlabel('Brain image',fontsize=F)
ax1.set_yticks(np.arange(len(list_y1)))
ax1.set_yticklabels(list_y1)
ax1.set_xticks(np.arange(len(list_x)))
ax1.set_xticklabels(list_x)  
ax1.set_ylabel('class',fontsize=F)
ax2 = ax1.twinx()
ax2.set_yticks(np.arange(len(list_y2)))
ax2.set_yticklabels(list_y2, va='baseline')
ax2.set_ylabel('size',fontsize=F)
#Rotate the tick labels and set their alignment.
plt.setp(ax1.get_xticklabels(), rotation=45, ha="right",
     rotation_mode="anchor")
plt.imshow(Class1, aspect='auto')
fig.tight_layout()
plt.show()

将进行以下绘制:

plot

此问题是第一个y轴无法正确拟合该图。第一个y轴的刻度之间的长度应与第二个y轴的刻度相同,并且顶部的0和底部的38应当在y轴上向内移动一点。希望你们中的一个能帮助我。

1 个答案:

答案 0 :(得分:0)

我最近遇到了同样的问题,发现了以下解决方法:为双轴ax2和父轴(ax1)设置相同的限制。这样可以将两个y轴上的刻度线对齐,然后根据需要设置刻度和标签。

ax2 = ax1.twinx()
ax2.set_ylim(ax1.get_ylim()) # This was the fix for me
ax2.set_yticks(np.arange(len(list_y2)))
ax2.set_yticklabels(list_y2, va='baseline')
ax2.set_ylabel('size',fontsize=F)

让我知道它是否有效。