matplotlib pyplot通过重叠来比较两个条形图

2024-09-27 00:23:08 发布

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

我有两个向量(r1和r2),长度都是3500,我想比较一下。问题是当我使用plt.bar时,我得到了两种不同的r2图。怎么可能?在

enter image description here

谁能告诉我我的代码有什么问题吗?在

def compare_representations(r1, r1title, r2, r2title, image, k):
    ka = np.asarray(range(k)) #ka =3500

    plt.figure(figsize=(13,10))
    #histogram Query
    hiq = plt.subplot(2,2,1)
    hiq.set_title("Histogram for " + r1title)
    hiq.set_xlabel("Visual words")
    hiq.set_ylabel("Frequency")
    #hist1 = plt.plot(r1, color='orangered')
    hist1 = plt.bar(ka,r1,width=1.0,color="orangered")


    #histogram Image
    his = plt.subplot(2,2,2)
    his.set_title("Histogram for "+ r2title)
    his.set_xlabel("Visual words")
    his.set_ylabel("Frequency")
    #hist2 = plt.plot(r2, color='mediumslateblue')
    hist2 = plt.bar(ka,r2,width=1.0,color='mediumslateblue')


    #histograms compared
    comp = plt.subplot(2,2,3)
    comp.set_title("Compare Histograms: ")
    comp.set_xlabel("Visual words")
    comp.set_ylabel("Frequency")
    #plt.plot(r1, color ='orangered')
    #plt.plot(r2, color = 'mediumslateblue')
    plt.bar(ka,r1,width=1.0,color ='orangered')
    plt.bar(ka,r2,width=1.0,color = 'mediumslateblue')


    #plot founded image
    ax = plt.subplot(2,2,4)
    ax.grid(False)
    img = mpimg.imread(image, format='jpeg')
    # Turn off tick labels and show just name of founded image
    ax.set_yticklabels([])
    ax.set_xticklabels([])
    ax.set_xlabel(os.path.basename(image))

    imgplot = plt.imshow(img)

    plt.show()

    return(hist1, hist2, imgplot)

Tags: imageplotbarpltaxwidthcolorr2
1条回答
网友
1楼 · 发布于 2024-09-27 00:23:08

我找不到关于plt.bar()的解决方案,我认为它不是适合您数据的绘图类型。我建议使用plt.plot()或{},使用{}进行比较。在

这里有一个例子(请注意,我已经删除了图像部分

def compare_representations(r1, r1title, r2, r2title, k):
    ka = np.asarray(range(k)) #ka =3500


    #histogram Query
    hiq = plt.subplot(2,2,1)
    hiq.set_ylim([0, 0.15])
    hiq.set_title("Histogram for " + r1title)
    hiq.set_xlabel("Visual words")
    hiq.set_ylabel("Frequency")
    hist1 = plt.plot(ka,r1,color="orangered")


    #histogram Image
    his = plt.subplot(2,2,2)
    his.set_ylim([0, 0.15])
    his.set_title("Histogram for "+ r2title)
    his.set_xlabel("Visual words")
    his.set_ylabel("Frequency")
    hist2 = plt.plot(ka,r2,color='mediumslateblue')


    #histograms compared
    plt.figure(figsize=(13,10))
    plt.ylim([0,0.15])
    plt.title("Compare Histograms: ")
    plt.xlabel("Visual words")
    plt.ylabel("Frequency")
    plt.plot(ka,r1,color="orangered", alpha=0.5)
    plt.plot(ka,r2,color='mediumslateblue', alpha=0.5)
    plt.show()


    return(hist1, hist2)

相关问题 更多 >

    热门问题