如何绘制不同形状的数组?

时间:2018-09-19 15:32:39

标签: python matplotlib

我试图绘制两个形状不同的数组,但无法将其中一个投影到另一个的顶部。例如:

#importing the relevant packages
import numpy as np
import matplotlib.pyplot as plt


def overplot(data1,data2):
    '''
    This function should make a contour plot
    of data2 over the data1 plot.
    '''

    #creating the figure
    fig = plt.figure()

    #adding an axe
    ax = fig.add_axes([1,1,1,1])

    #making the plot for the
    #first dataset
    ax.imshow(data1)

    #overplotting the contours
    #for the second dataset
    ax.contour(data2, projection = data2,
               levels = [0.5,0.7])

    #showing the figure
    plt.show(fig)

    return


if __name__ == '__main__':
    '''
    testing zone
    '''

    #creating two mock datasets
    data1 = np.random.rand(3,3)
    data2 = np.random.rand(9,9)

    #using the overplot
    overplot(data1,data2)

当前,我的输出如下:

example image

我实际上想要将第二个数据集的轮廓投影到第一个数据集中。这样,如果我得到的是同一物体的图像,但相机的分辨率不同,则可以绘制这些图。我怎样才能做到这一点?

感谢您的时间和精力。

1 个答案:

答案 0 :(得分:1)

通常最好使数据匹配,然后再绘制。这样,您可以完全控制事情的完成方式。

在您给出的简单示例中,您可以沿每个轴重复使用以扩展3x3数据以匹配9x9数据。也就是说,您可以使用data1b = np.repeat(np.repeat(data1, 3, axis=1), 3, axis=0)给出: enter image description here

但是对于图像更有趣的情况,如您在问题末尾提到的那样,轴可能不是整数倍,并且样条曲线或其他类型插值会更好地为您服务。这种差异说明了为什么最好自己控制它,因为有很多方法可以进行这种类型的映射。