如何制作条形图的合并版本?

问题描述 投票:0回答:2

我有一个列表列表

A = [[8.4476, 1.2896e-06], [8.1652, 2.4992e-09], [8.4757, 3.4313e-07], [8.5289, 2.8196e-09], [8.2903, 4.0126e-09], [8.2122, 1.2891e-08], [8.2598, 4.4194e-09], [8.2133, 1.3551e-09], [8.0199, 2.7166e-09], [8.5444, 4.5322e-09], [8.2125, 0.013257], [8.2465, 9.6889e-08], [8.3649, 2.2352e-08], [8.5861, 2.685e-08], [8.3939, 6.6243e-10], [8.1729, 1.2449e-08], [8.1729, 8.6429e-09], [8.0997, 3.1144e-09], [8.3379, 7.308e-08], [8.3071, 3.3383e-08], [8.5703, 5.1506e-10], [8.4036, 7.765e-09], [8.4314, 2.471e-08], [8.1761, 1.9816e-09], [8.5305, 1.7541e-09], [8.1103, 1.6479e-09], [8.1818, 4.3065e-05], [8.2028, 8.5556e-10], [8.4936, 5.0891e-09], [8.0726, 9.2431e-10], [8.5635, 2.9602e-07], [8.2327, 1.8363e-08], [8.1055, 5.482e-10], [8.2185, 1.7178e-05], [8.1212, 6.7481e-09], [8.092, 6.8427e-08], [8.0496, 2.6637e-09], [8.1818, 4.4151e-05], [8.1028, 1.5019e-09], [8.5635, 1.1081e-07], [8.4396, 7.4304e-06], [8.5011, 0.00048155], [8.1179, 3.244e-08], [8.0888, 2.0115e-09], [8.2783, 2.1645e-09], [8.0069, 3.2472e-09], [8.2028, 9.4631e-10], [8.1818, 4.3449e-05], [8.5635, 1.0127e-07], [8.0726, 1.0427e-09], [8.0542, 2.136e-09], [8.1179, 5.8536e-08], [8.3162, 6.0933e-09], [8.1004, 3.3053e-08], [8.4467, 1.0477e-08], [8.7168, 7.4619e-08], [8.8625, 8.2386e-08], [9.0955, 1.0668e-05], [8.9417, 1.1475e-07], [8.9012, 2.8507e-07], [8.6043, 4.9697e-07], [8.9169, 2.889e-06], [8.6292, 2.3711e-09], [8.8612, 9.6603e-08], [8.6301, 1.5749e-07], [8.8612, 5.4537e-08], [8.9189, 1.1827e-05], [8.6571, 4.5097e-10], [9.0022, 3.7623e-08], [8.8702, 4.1319e-08], [8.6571, 6.9032e-10], [9.0022, 1.2643e-08], [8.7583, 8.7156e-08], [8.6976, 0.021634], [9.2245, 6.5853e-09], [9.2596, 1.4178e-09], [9.7357, 4.5818e-07], [9.7357, 1.5467e-06], [9.2596, 1.0373e-09], [9.7357, 1.352e-06], [9.6335, 3.8465e-08], [9.4126, 3.5259e-08], [9.6335, 2.8004e-08], [9.4126, 7.7311e-08], [9.4126, 5.416e-08], [9.2529, 5.0376e-07], [9.6335, 3.388e-08], [9.4777, 3.0358e-07], [9.2448, 5.8716e-09], [9.3581, 1.3008e-08], [9.3581, 6.5036e-09], [9.7433, 1.1734e-07], [9.3581, 7.459e-09], [9.4874, 3.4768e-08], [9.6671, 4.8428e-08], [9.6671, 5.1251e-08], [9.4874, 3.5968e-08], [9.6671, 5.7862e-08], [9.4874, 4.2036e-08], [9.8819, 2.3973e-08], [9.8819, 2.8353e-08], [9.8819, 3.0334e-08], [10.0615, 9.0102e-06], [10.0615, 9.7216e-06], [10.0615, 6.8295e-06], [10.086, 0.00021523], [10.3562, 0.84578], [9.8411, 0.00087999], [10.3562, 0.8662], [10.086, 0.00044829], [9.8411, 0.00090252], [10.3562, 0.91307], [10.086, 0.00010695], [9.8411, 0.00088925], [12.0191, 0.0068726], [12.0191, 0.007106], [12.0191, 0.0070258], [11.7748, 0.039574], [11.7748, 0.042754], [11.7748, 0.036293], [11.9927, 0.045817], [11.9927, 0.0523], [11.9927, 0.049076], [11.9587, 0.064393], [11.9587, 0.068927], [11.9587, 0.061129], [11.8561, 0.033099], [11.8561, 0.028988], [11.8561, 0.034371]]

我想绘制条形图,其中我的x轴是这些列表的第一个元素,而我的y轴是这些列表的第二个元素。但是,我的x轴需要合并到7个容器中,例如[8,8.6],[8.6,9.2],[9.2,9.8],[9.8,10.4],[10.4,11]和[11,12.2]。条的高度需要是每个x仓中落入的第二元素的平均值,包括每个条的误差线,该误差棒代表每个x仓中落入的第二元素的变化。

我所能做的就是将所有数据点绘制为离散分布。我无法合并7个箱以便绘制更少的条形。非常感谢您的帮助。enter image description here

python-3.x matplotlib plot bar-chart histogram
2个回答
2
投票

如果可以考虑其他软件包,我建议pandas

import pandas as pd

df = pd.DataFrame(A)

(df.iloc[:,1]
   .groupby(pd.cut(df.iloc[:,0],bins=[8,8.6,9.2,9.8,10.4, 11,12.2]))
   .mean()
   .plot.bar()
)

输出:

enter image description here


1
投票

首先可以将值放入垃圾箱。然后可以计算每个仓的平均值和标准偏差。

可以使用均值和x位置绘制条。轮廓线可以标记标准偏差。需要更改zorder,以确保将条形图绘制在条形图的后面。

此外,实际数据点可以作为散点图添加。随机抖动有助于涂抹一致点。

import numpy as np
import matplotlib.pyplot as plt

A = [[8.4476,1.2896e-06],[8.1652,2.4992e-09],[8.4757,3.4313e-07],[8.5289,2.8196e-09],[8.2903,4.0126e-09],[8.2122,1.2891e-08],[8.2598,4.4194e-09],[8.2133,1.3551e-09],[8.0199,2.7166e-09],[8.5444,4.5322e-09],[8.2125,0.013257],[8.2465,9.6889e-08],[8.3649,2.2352e-08],[8.5861,2.685e-08],[8.3939,6.6243e-10],[8.1729,1.2449e-08],[8.1729,8.6429e-09],[8.0997,3.1144e-09],[8.3379,7.308e-08],[8.3071,3.3383e-08],[8.5703,5.1506e-10],[8.4036,7.765e-09],[8.4314,2.471e-08],[8.1761,1.9816e-09],[8.5305,1.7541e-09],[8.1103,1.6479e-09],[8.1818,4.3065e-05],[8.2028,8.5556e-10],[8.4936,5.0891e-09],[8.0726,9.2431e-10],[8.5635,2.9602e-07],[8.2327,1.8363e-08],[8.1055,5.482e-10],[8.2185,1.7178e-05],[8.1212,6.7481e-09],[8.092,6.8427e-08],[8.0496,2.6637e-09],[8.1818,4.4151e-05],[8.1028,1.5019e-09],[8.5635,1.1081e-07],[8.4396,7.4304e-06],[8.5011,0.00048155],[8.1179,3.244e-08],[8.0888,2.0115e-09],[8.2783,2.1645e-09],[8.0069,3.2472e-09],[8.2028,9.4631e-10],[8.1818,4.3449e-05],[8.5635,1.0127e-07],[8.0726,1.0427e-09],[8.0542,2.136e-09],[8.1179,5.8536e-08],[8.3162,6.0933e-09],[8.1004,3.3053e-08],[8.4467,1.0477e-08],[8.7168,7.4619e-08],[8.8625,8.2386e-08],[9.0955,1.0668e-05],[8.9417,1.1475e-07],[8.9012,2.8507e-07],[8.6043,4.9697e-07],[8.9169,2.889e-06],[8.6292,2.3711e-09],[8.8612,9.6603e-08],[8.6301,1.5749e-07],[8.8612,5.4537e-08],[8.9189,1.1827e-05],[8.6571,4.5097e-10],[9.0022,3.7623e-08],[8.8702,4.1319e-08],[8.6571,6.9032e-10],[9.0022,1.2643e-08],[8.7583,8.7156e-08],[8.6976,0.021634],[9.2245,6.5853e-09],[9.2596,1.4178e-09],[9.7357,4.5818e-07],[9.7357,1.5467e-06],[9.2596,1.0373e-09],[9.7357,1.352e-06],[9.6335,3.8465e-08],[9.4126,3.5259e-08],[9.6335,2.8004e-08],[9.4126,7.7311e-08],[9.4126,5.416e-08],[9.2529,5.0376e-07],[9.6335,3.388e-08],[9.4777,3.0358e-07],[9.2448,5.8716e-09],[9.3581,1.3008e-08],[9.3581,6.5036e-09],[9.7433,1.1734e-07],[9.3581,7.459e-09],[9.4874,3.4768e-08],[9.6671,4.8428e-08],[9.6671,5.1251e-08],[9.4874,3.5968e-08],[9.6671,5.7862e-08],[9.4874,4.2036e-08],[9.8819,2.3973e-08],[9.8819,2.8353e-08],[9.8819,3.0334e-08],[10.0615,9.0102e-06],[10.0615,9.7216e-06],[10.0615,6.8295e-06],[10.086,0.00021523],[10.3562,0.84578],[9.8411,0.00087999],[10.3562,0.8662],[10.086,0.00044829],[9.8411,0.00090252],[10.3562,0.91307],[10.086,0.00010695],[9.8411,0.00088925],[12.0191,0.0068726],[12.0191,0.007106],[12.0191,0.0070258],[11.7748,0.039574],[11.7748,0.042754],[11.7748,0.036293],[11.9927,0.045817],[11.9927,0.0523],[11.9927,0.049076],[11.9587,0.064393],[11.9587,0.068927],[11.9587,0.061129],[11.8561,0.033099],[11.8561,0.028988],[11.8561,0.034371]]
A = np.array(A)

numbins = 7
xmin = 8
xmax = xmin + numbins * 0.6
xrange = xmax - xmin
bounds = np.linspace(xmin, xmax, numbins + 1, endpoint=True)
mids = (bounds[:-1] + bounds[1:]) / 2
bins = [[] for _ in range(numbins)]
for x, y in A:
    bins[int((x - xmin) / xrange * numbins)].append(y)
means = np.array([np.mean(bin) if len(bin) > 0 else np.nan for bin in bins])
stds = np.array([np.std(bin) if len(bin) > 0 else np.nan for bin in bins])

plt.stem(mids, means + stds, linefmt='k-', markerfmt='k_', use_line_collection=True)
plt.bar(mids, means, width=xrange / numbins, color='salmon', ec='k', zorder=2)
plt.scatter(A[:, 0]+np.random.uniform(-.02, .02, A.shape[0]), A[:, 1],
            s=2, color='b', alpha=0.5, zorder=3)
plt.xticks(bounds)
plt.yscale('log')
plt.show()

example plot

PS:另一个选项使用相同的数据创建boxplots

plt.boxplot(bins, positions=mids, showfliers=False)
plt.scatter(A[:, 0]+np.random.uniform(-.02, .02, A.shape[0]), A[:, 1],
            s=2, color='b', alpha=0.5, zorder=3)
plt.xlim(bounds[0]-.1, bounds[-1]+.1)
plt.xticks(bounds, [f'{b:.1f}' for b in bounds])
plt.yscale('log')
plt.show()

boxplot example

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