在Python中使用Matplotlib的条形图 [英] Bar Chart Using Matplotlib in Python

查看:46
本文介绍了在Python中使用Matplotlib的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配对列表.

示例:

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

在Python中,我需要使用 matplotlib 生成条形图,以便如果 x y 坐标在一对中相同,则条形图应达到最大程度,否则,如果(x,y)不同,则条形图应处于0级.所以请帮助我用 Python 编写代码.

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.

推荐答案

您首先需要找出哪些对相等并生成这些结果的列表.然后可以使用 matplotlib.pyplot.bar 绘制此列表.

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()

此代码产生以下图:

这篇关于在Python中使用Matplotlib的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆