散点图的矩阵元素
Matrix elements for scatter plot
我有这个数组:
b=np.array([1,2,3])
这个矩阵:
a=np.array([[ 4, 2, 12],
[ 7, 12, 0],
[ 10, 7, 10]])
我现在想画一个散点图,以b[i]为x轴,a[j][i]为y轴。更具体地说,我希望情节中的 points/coordinates 为:
(b[i],a[j][i])
我的情况是:
(1,4) (1,7) (1,10) (2,2) (2,12) (2,7) (3,12) (3,0) (3,10)
然后我可以很容易地绘制出来。情节看起来像这样:
任何人都可以帮助我为我的情节创建要点吗?有通用的解决方案吗?
import matplotlib.pyplot as p
import numpy as np
b=np.array([1,2,3])
a=np.array([[ 4, 2, 12],
[ 7, 12, 0],
[ 10, 7, 10]])
p.plot(b,a[0],'o-')# gives you different colors for different datasets
p.plot(b,a[1],'o-')# showing you things that scatter won't
p.plot(b,a[2],'o-')
p.xlim([0.5,3.5])
p.ylim([-1,15])
p.show()
您可以将矩阵重塑为向量,然后对它们进行散点图绘制:
# repeat the b vector for the amount of rows a has
x = np.repeat(b,a.shape[0])
# now reshape the a matrix to generate a vector
y = np.reshape(a.T,(1,np.product(a.shape) ))
# plot
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
结果:
我有这个数组:
b=np.array([1,2,3])
这个矩阵:
a=np.array([[ 4, 2, 12],
[ 7, 12, 0],
[ 10, 7, 10]])
我现在想画一个散点图,以b[i]为x轴,a[j][i]为y轴。更具体地说,我希望情节中的 points/coordinates 为:
(b[i],a[j][i])
我的情况是:
(1,4) (1,7) (1,10) (2,2) (2,12) (2,7) (3,12) (3,0) (3,10)
然后我可以很容易地绘制出来。情节看起来像这样:
任何人都可以帮助我为我的情节创建要点吗?有通用的解决方案吗?
import matplotlib.pyplot as p
import numpy as np
b=np.array([1,2,3])
a=np.array([[ 4, 2, 12],
[ 7, 12, 0],
[ 10, 7, 10]])
p.plot(b,a[0],'o-')# gives you different colors for different datasets
p.plot(b,a[1],'o-')# showing you things that scatter won't
p.plot(b,a[2],'o-')
p.xlim([0.5,3.5])
p.ylim([-1,15])
p.show()
您可以将矩阵重塑为向量,然后对它们进行散点图绘制:
# repeat the b vector for the amount of rows a has
x = np.repeat(b,a.shape[0])
# now reshape the a matrix to generate a vector
y = np.reshape(a.T,(1,np.product(a.shape) ))
# plot
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
结果: