MatPlotLib中确保在子图中使用imshow()绘图时子图等高

在MatPlotLib中使用matplotlib.pyplot.subplots()函数建立子图,并在子图中使用matplotlib.axes.Axes.imshow()函数绘制颜色图时,可能出现绘制的子图高度不一致的现象,影响绘图结果:

import matplotlib
import matplotlib.pyplot as plt
 
from numpy import random
 
# Generate random matrixes
arrRandomMatrix1 = random.random(size=(25,45))
arrRandomMatrix2 = random.random(size=(45,45))
 
# Creating subplots
figAxesToPlot, arrAxesToPlot = plt.subplots(nrows=1, ncols=2, sharey=False, squeeze=False)
 
# Plotting
imPlot1=arrAxesToPlot[0][0].imshow(X=arrRandomMatrix1)
imPlot2=arrAxesToPlot[0][1].imshow(X=arrRandomMatrix2)
plt.colorbar(imPlot1, ax=arrAxesToPlot[0][0])
plt.colorbar(imPlot2, ax=arrAxesToPlot[0][1])
plt.show()

这是因为MatPlotLib在处理imshow()绘图时,默认将保证图中的每个像素点为正方形。当子图数据矩阵具有不同的尺度时,MatPlotLib会缩放子图,以保证每张子图中的像素点均为正方形。

调用imshow()绘图时,传入aspect="auto"参数,可以覆盖该默认设置,此时,MatPlotLib不会再尝试保证子图中的像素点为正方形:

import matplotlib
import matplotlib.pyplot as plt
 
from numpy import random
 
# Generate random matrixes
arrRandomMatrix1 = random.random(size=(25,45))
arrRandomMatrix2 = random.random(size=(45,45))
 
# Creating subplots
figAxesToPlot, arrAxesToPlot = plt.subplots(nrows=1, ncols=2, sharey=False, squeeze=False)
 
# Plotting
imPlot1=arrAxesToPlot[0][0].imshow(X=arrRandomMatrix1, aspect="auto")
imPlot2=arrAxesToPlot[0][1].imshow(X=arrRandomMatrix2, aspect="auto")
plt.colorbar(imPlot1, ax=arrAxesToPlot[0][0])
plt.colorbar(imPlot2, ax=arrAxesToPlot[0][1])
plt.show()

您可以通过覆盖rcParams["image.aspect"]全局设置改变MatPlotLib的默认子图缩放行为:

plt.rc("image", aspect="auto")
it
除非特别注明,本页内容采用以下授权方式: Creative Commons Attribution-ShareAlike 3.0 License