0

I have a list of pairs.

Example:

pair1=[(a,a),(b,b),(c,c),(x,y)....]

In Python i need to generate a bar chart using matplotlib such that if x and y co-ordinates are same in a pair the bar chart should come to the maximum extent or else in case of (x,y) they are different and hence the bar chart should be at the 0 level. So please do help me out with the code in Python.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Vinith
  • 23
  • 9

1 Answers1

1

You first need to find out which pairs equal and generate a list of those results. This list can then be plotted using matplotlib.pyplot.bar.

import matplotlib.pyplot as plt

pair1=[("a","a"),("2",2),("b","b"),("c","c"),("x","y")]

f = lambda t: t[0] == t[1]
y= list(map(f, pair1))

plt.bar(range(len(y)), y)
plt.yticks([])
plt.show()

This code produces the following plot:

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • how to add scrolling bar to this if the series is long, equal spacing between 2 adjacent bars, same colour and printing the x value exaclty below its bar and removing the y ticks....do help me ..since I m new to Python – Vinith Mar 20 '17 at 12:51
  • This is not a code writing service. While the code already has most of those features in, for each of the questions there are answers around somewhere. Scrollbars are not easy, you may start looking at a scrollbar for [QT](http://stackoverflow.com/questions/42622146/scrollbar-on-matplotlib-showing-page), [wxpython](http://stackoverflow.com/questions/38559270/matplotlib-scrolling-plot) or using a [SliderWidget](http://stackoverflow.com/questions/14668454/matplotlib-scroll-bar) – ImportanceOfBeingErnest Mar 20 '17 at 13:15
  • WTF if you dont know please .. @ImportanceOfBeingErnest – Vinith Mar 20 '17 at 17:36
  • Sure I know it. I wrote for example [this answer](http://stackoverflow.com/questions/42622146/scrollbar-on-matplotlib-showing-page). You may copy it and use it if you like. – ImportanceOfBeingErnest Mar 20 '17 at 17:50
  • but where can i include the above code of urs there – Vinith Mar 20 '17 at 17:57