我正在尝试使用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))

如果我只运行最后一行的所有内容,则可以使阵列完全可视化:

Matplotlib:用第二个y轴显示-LMLPHP

但是,如果添加第二个y轴(ax2 = ax1.twinx())作为散点图的准备工作,它将更改为以下不完整渲染:

Matplotlib:用第二个y轴显示-LMLPHP

有什么问题?我在上面的代码中保留了几行描述了散点图的添加,尽管这似乎并不是问题的一部分。

最佳答案

在Thomas Kuehn指出的GitHub讨论之后,该问题已在几天前得到解决。在没有易于使用的内置文件的情况下,这是使用 Aspect ='auto'属性的修复程序。为了获得漂亮的常规框,我使用数组尺寸调整了图形x/y。轴自动缩放功能已用于删除一些其他白色边框。

oneDim = np.array([0.5,1,2.5,3.7])
twoDim = np.random.rand(8,4)

plt.figure(figsize=(twoDim.shape[1]/2,twoDim.shape[0]/2))
ax1 = plt.gca()

ax1.imshow(twoDim, cmap='Purples', interpolation='nearest', aspect='auto')
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()

ax2 = ax1.twinx()

#Required to remove some white border
ax1.autoscale(False)
ax2.autoscale(False)

结果:

Matplotlib:用第二个y轴显示-LMLPHP

关于Matplotlib:用第二个y轴显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48255824/

10-11 03:57