如何使用元组绘制列表[重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

我试图绘制一个条形图,其中x轴表示每个元组的第一个元素,y轴是每个元组的第二个元素。

与这篇文章非常相似:Using Counter() in Python to build histogram?

arr = [(0, 152),
     (1, 106),
     (2, 71),
     (3, 89),
     (4, 69),
     (5, 83),
     (6, 139),
     (7, 141),
     (8, 164),
     (9, 75),
     (10, 98)]

我怎样才能做到这一点?

到目前为止我有这个:

输入:

plt.bar(counts.items())

输出:

TypeError: <lambda>() takes at least 2 arguments (1 given)

谢谢 :)

python list matplotlib tuples
1个回答
0
投票

一个似乎运行良好的快速解决方案是将数据加载到Panda的DataFrame中并使用它的绘图功能:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

x = [(0, 152),
     (1, 106),
     (2, 71),
     (3, 89),
     (4, 69),
     (5, 83),
     (6, 139),
     (7, 141),
     (8, 164),
     (9, 75),
     (10, 98)]

pd.DataFrame(x, columns=['lbl','val']).set_index('lbl').plot(kind='bar');

how_the_above_code_renders_the_chart

© www.soinside.com 2019 - 2024. All rights reserved.