python - 在直方图中绘制列表列表 - x 和 y 值

标签 python matplotlib

我有一个列表列表:

[[Decimal('3.69578'), Decimal('5.12519')], [Decimal('3.69586'),Decimal('2.50457468')], [Decimal('3.69587'), Decimal('72.33894358')], [Decimal('3.69629'), Decimal('54.2359599')], ...]

我想使用 matplotlib 将子列表中的一个值作为 x 轴,将另一个值作为 y 轴,将其绘制成直方图。

plt.hist(list)

给我:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2896, in hist
stacked=stacked, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 5652, in hist
    m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 176, in histogram
    mn, mx = [mi+0.0 for mi in range]
TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'

最佳答案

你可以将它们绘制成 float

plt.hist([[float(x) for x in y] for y in list])

关于python - 在直方图中绘制列表列表 - x 和 y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37871683/

相关文章:

python - 在 Django 中重用 filter() 查询集

Python "private"函数编码约定

python - 无法绘制图表 : matplotlib is needed for plotting

python - matplotlib 不在散点图中显示图例

python - 如何让 XKCD 字体在 matplotlib 中工作

python - 当 DynamoDB 中不存在项目时,如何使用 Python (boto3) 强制 delete_item 返回错误?

python - 如何在 numpydoc 中输入 int 列表

python - 加快正态分布概率质量分配

python - Matplotlib:将两个 y 轴围绕零对齐

python - 如何从 matplotlib 图中获取行数和列数?