3

I have two vectors (r1 and r2) both of length 3500 and I want to compare them. The problem is that when I use plt.bar I get two different kind of plot for r2. How is it possible?

enter image description here

Can anyone tell me what is wrong in my code?

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)
Furin
  • 532
  • 10
  • 31
  • Can you share the input data? – alec_djinn May 15 '18 at 14:26
  • hi! [here](https://gitlab.com/rosaverlag/share) you can find the file (.npy ) with the vectors of all database (the r2 is one of this vectors). r1 is in the .txt file. k = 3500. The image is not necessary. Thanks! – Furin May 15 '18 at 15:12
  • Using my own (reduced) variable values (r1, r1title, r2, r2title,) the code worked properly for me – daniel_hck May 15 '18 at 18:08
  • oh, that's weird...How much did you reduce the values? – Furin May 16 '18 at 08:06
  • @RoRy The .npy file is a binary one, I cannot read it, can you send the r2 vector as csv or similar format? Even better if you just `pickle.dump()` both r1 and r2 so I am sure I am using the same inputs you are using. – alec_djinn May 16 '18 at 08:07
  • @alec_djinn I pushed the two .csv file to the repository. Thanks for the help. Both r1 and r2 type is numpyarray. – Furin May 16 '18 at 15:04
  • @RoRy I can confirm the odd behavior, I am now checking for the problem and hopefully a solution. – alec_djinn May 18 '18 at 10:09

2 Answers2

0

I couldn't find a solution regarding plt.bar(), I don't think it is the right plot-type for your data. I would suggest plt.plot() or plt.scatter() instead, using alpha=0.5 for the comparison.

Here an example (mind that I have removed the image part

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_djinn
  • 10,104
  • 8
  • 46
  • 71
  • Thanks @alec_dijnn. I think both plt.plot and plt.bar are correct for my data. I also used plt.plot but with plt.bar I'm able to see the data more clearly. By the way, with plt.plot you do not need the variable ka. – Furin May 21 '18 at 16:04
0

@Furin ... this is not an answer: I'm just putting this here as a means of contacting you about a question of mine in which you showed some interest earlier this month. This one, about Python and caching of passwords.

I've added a "stub" of an answer... if you have the time and the inclination (I don't really have time right now) you could investigate this...

If you add a comment like "seen this" to this answer, I'll delete it.

mike rodent
  • 14,126
  • 11
  • 103
  • 157