Skip to main content
 首页 » 编程设计

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

2024年10月17日7youxin

我有一个列表列表:

[[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])