【问题标题】:matplotlib pyplot compare two bar diagrams by overlapping themmatplotlib pyplot 通过重叠比较两个条形图
【发布时间】:2018-10-25 10:10:14
【问题描述】:

我有两个长度均为 3500 的向量(r1 和 r2),我想比较它们。问题是当我使用plt.bar 时,我得到了两种不同类型的 r2 绘图。这怎么可能?

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

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)

【问题讨论】:

  • 可以分享输入数据吗?
  • 嗨! here 你可以找到包含所有数据库向量的文件(.npy)(r2 是这个向量之一)。 r1 在 .txt 文件中。 k = 3500。图像不是必需的。谢谢!
  • 使用我自己的(减少的)变量值(r1,r1title,r2,r2title,)代码对我来说正常工作
  • 哦,这很奇怪...您将值降低了多少?
  • @RoRy .npy 文件是二进制文件,我看不懂,能否将 r2 向量发送为 csv 或类似格式?如果你只是pickle.dump() r1 和 r2 就更好了,所以我确定我使用的输入与你使用的相同。

标签: python matplotlib bar-chart


【解决方案1】:

@Furin ...这不是一个答案:我只是把它放在这里,作为与您联系的一种方式,您在本月早些时候对我的一个问题表示了一些兴趣。 This one,关于 Python 和密码缓存。

我已经添加了a "stub" of an answer...如果您有时间和兴趣(我现在真的没有时间),您可以调查一下...

如果你在这个答案中添加“看到这个”之类的评论,我会删除它。

【讨论】:

    【解决方案2】:

    我找不到关于 plt.bar() 的解决方案,我认为它不适合您的数据。我建议改用plt.plot()plt.scatter(),使用alpha=0.5 进行比较。

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

    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)
    

    【讨论】:

    • 谢谢@alec_dijnn。我认为 plt.plot 和 plt.bar 对我的数据都是正确的。我也使用了 plt.plot 但使用 plt.bar 我能够更清楚地看到数据。顺便说一句,使用 plt.plot 你不需要变量 ka。
    猜你喜欢
    • 2015-12-25
    • 2014-08-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2019-09-01
    • 2019-02-11
    相关资源
    最近更新 更多