我正在尝试使用 imshow() 在 matplotlib 中绘制二维数组,并在第二个 y 轴上用散点图覆盖它。
oneDim = np.array([0.5,1,2.5,3.7])
twoDim = np.random.rand(8,4)
plt.figure()
ax1 = plt.gca()
ax1.imshow(twoDim, cmap='Purples', interpolation='nearest')
ax1.set_xticks(np.arange(0,twoDim.shape[1],1))
ax1.set_yticks(np.arange(0,twoDim.shape[0],1))
ax1.set_yticklabels(np.arange(0,twoDim.shape[0],1))
ax1.grid()
#This is the line that causes problems
ax2 = ax1.twinx()
#That's not really part of the problem (it seems)
oneDimX = oneDim.shape[0]
oneDimY = 4
ax2.plot(np.arange(0,oneDimX,1),oneDim)
ax2.set_yticks(np.arange(0,oneDimY+1,1))
ax2.set_yticklabels(np.arange(0,oneDimY+1,1))
如果我只运行到最后一行的所有内容,我的数组就会完全可视化:
但是,如果我添加第二个 y 轴 (ax2=ax1.twinx()) 作为散点图的准备,它会更改为这种不完整的渲染:
有什么问题?我在上面的代码中留下了几行描述散点图的添加,尽管它似乎不是问题的一部分。