将多变量离散列表列表转换为散点图

时间:2020-05-24 20:57:19

标签: arrays python-3.x list numpy multidimensional-array

我有一个多值函数形式的列表列表

*[[var1, [a1,b1,...,z1], [var2, [a2,b2,...,z2]],,,[varn, [an,bn,,,,zn]]]*. 

我想先将其转换为

形式的多值列表
*[[var1,a1], [var1,b1],,,[var1,z1],[var2,a2],[var2,b2],,,[var2,z2],,,,]* 

因此我可以将它们绘制为散点图,并对其进行进一步分析。有更简单的方法吗?如果没有,您如何进行这种转换?

如果我正在处理列表的单值列表,这是我所学的(从我的How to make a binned version of a barplot?的这篇帖子中):

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()

谢谢

1 个答案:

答案 0 :(得分:0)

尝试:

[[x[0], x[1][i]] for x in A for i in range(len(x[1]))]