Vinith
Vinith

Reputation: 23

Bar Chart Using Matplotlib in Python

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.

Upvotes: 0

Views: 646

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

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

Upvotes: 1

Related Questions