我有一个多值函数形式的列表
*[[var1, [a1,b1,...,z1], [var2, [a2,b2,...,z2]],,,[varn, [an,bn,,,,zn]]]*.
我想先将其转换为一个多值列表,形式为
*[[var1,a1], [var1,b1],,,[var1,z1],[var2,a2],[var2,b2],,,[var2,z2],,,,]*
这样我就可以把这些绘制成散点图,然后用它们做进一步的分析。有没有更简单的方法?如果没有,你是如何进行这样的转换的?
如果我处理的是一个单值列表,下面是我所学到的(从我的这个帖子中得到的)。如何制作一个二进制版本的条形图?):
import numpy as np
import matplotlib.pyplot as plt
A = [[var1,val1], [var2,val2], ...[varn,valn]] # Some single value list of lists
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)
bins = [np.array(b) for b in bins]
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, [f'{b:.1f}' for b in bounds])
plt.yscale('log')
plt.show()
谢谢你
试试。
[[x[0], x[1][i]] for x in A for i in range(len(x[1]))]