使用Matplotlib组织轴

2024-05-19 01:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Matplotlib和Python绘制由直线连接的关节(数据来自坐标文本文件,表示(x,y)关节),并排绘制每个关节到其他关节的距离矩阵,绘制的每个骨骼由相同文本文件数据的计算生成。在

现在,我已经绘制了骨骼,连接到按键事件,也就是说,在每次读取文本文件时,都会绘制出多个骨架,从而产生一个运动,而且这种运动是可以感知的,因为用户必须按右箭头键来逐个绘制骨骼序列,从那时起,这种运动就一直可见。在

另外,我已经构建了矩阵,并且在代码中进行了一些计算之后,每个矩阵都用蓝色的比例尺绘制,旁边还有一个色条。我已经测试了每一个矩阵的绘图,它工作得很好。注意,如果我在一个文本文件中有43个骨架,那么必须有43个矩阵,每个骨架对应一个。在

问题是:我用散射法绘制骨骼,只有一个图形和一个轴。我必须在骨架之外绘制每个矩阵,并且我使用imshow将数组中的矩阵绘制成图形,以图像的形式,就像图像一样。但我不知道如何在不添加另一个轴的情况下,按顺序组织轴并绘制矩阵,而不必添加另一个轴,如fig,(ax1,ax)=plt.子批次例如,(ncols=2)。由于我是Matplotlib的初学者,我对如何在不需要重新构建绘制骨架的方法的情况下并排绘制骨架有点困惑。在

代码:

from matplotlib import gridspec
import matplotlib.pyplot as plt
import numpy as np

movement = np.loadtxt("file01.txt")

bone_list = [[1, 3], [2, 3], [3, 4], [4, 7], [5, 7], [6, 7], [1, 8], [2, 9], 
[8, 10], [9, 11], [10, 12], [11, 13], [5, 14], [6, 15], [14, 16], [15, 17], 
[16, 18], [17, 19], [3, 20]]

bone_list = np.array(bone_list) - 1
number_of_postures = int(len(movement)/20)
list_for_matrix = []


for i in range(number_of_postures):

    list_for_matrix.append(movement[(i*20):((i+1)*20),:3]) 
    matrixCoord = np.array(list_for_matrix)
    matrixDistance= np.zeros((number_of_postures,20,20))


for k in range(number_of_postures): 
    for i in range(len(matrixDistance[0])): 
        for j in range(len(matrixDistance[0])):     
            matrixDistance[k,i,j] = np.linalg.norm(matrixCoord[k,i,:] - 
            matrixCoord[k,j,:])


fig, ax = plt.subplots()

i = 1 #this variable is for plotting from the second skeleton on with the function call.

def press(event):
global i

if event.key == 'right':        
    fig.canvas.draw_idle()  
    plt.cla()   
    plt.xlim(100, 190)  
    plt.ylim(-250, 0)   
    skeleton = movement[i*20:(i+1)*20]      
    x = skeleton[:, 0]      
    y = -skeleton[:, 1]     
    sc = ax.scatter(x, y, s=40) 
    for bone in bone_list:      
        ax.plot([x[bone[0]], x[bone[1]]], [y[bone[0]], y[bone[1]]], 'r')

    plt.subplots_adjust(left=0.12, bottom=0.11, right=0.49, top=0.93,wspace=0.20, hspace=0.20)

    if i < number_of_postures:  
        i+=1

    else:           
        return


    #plot of the first skeleton, outside the function call.
    plt.xlim(100, 190)
    plt.ylim(-250, 0)
    skeleton = movement[0*20:(0+1)*20]
    x = skeleton[:, 0]
    y = -skeleton[:, 1]
    sc = ax.scatter(x, y, s=40)
    for bone in bone_list   
        ax.plot([x[bone[0]], x[bone[1]]], [y[bone[0]], y[bone[1]]], 'r')
    plt.subplots_adjust(left=0.12, bottom=0.11, right=0.49,top=0.93,wspace=0.20, hspace=0.20)


    fig.canvas.mpl_connect("key_press_event", press)
    plt.show()


    #that's the plotting for the matrices, outside the plt.show(), since it's not working properly.
    '''
    img = matrixDistance[i]
    imgplot = plt.imshow(img)
    plt.title('Distance Matrix')
    imgplot.set_cmap('PuBu')
    plt.colorbar()

    '''

下面是两张图片,代码生成的结果,以及我需要实现的目标。在

代码生成的结果:

Result generated by the code

我需要实现的目标:

What I need to achieve yet


Tags: oftheinnumberfornp绘制plt
1条回答
网友
1楼 · 发布于 2024-05-19 01:47:39

大多数影响子批次的命令会替换同一区域中存在的任何轴。为你创造了一条线条

fig, ax = plt.subplots()

随后对subplotssubplot的调用将删除此轴并创建一个新轴。您需要同时设置两个轴。你可以执行

^{pr2}$

然后使用

^{3}$

在当前代码之前。这将使ax1轴matplotlib绘制。在当前代码之后和绘制矩阵的代码之前,再次使用命令来设置ax2(即sca(ax2))。在

相关问题 更多 >

    热门问题