我有两个numpy数组,想要在同一个图中绘制它们
我目前的代码如下所示
X1 # numpy array
X2 # numpy array
X1Df = pd.DataFrame(columns=['x','y'])
k = 0
for i in X1: # copy second numpy to DataFrome
temp = pd.DataFrame({'x' : i[0],
'y' : i[1]},index=[k])
k = k + 1
X1Df = pd.concat([X1Df,temp])
X1Df.plot(kind='scatter',x = 'x',y='y')
X2Df = pd.DataFrame(columns=['x','y'])
k = 0
for i in X2: ## Copy second numpy to DataFrame
temp = pd.DataFrame({'x' : i[0],
'y' : i[1]},
index=[k])
k = k + 1
X2Df = pd.concat([X2Df,temp])
X2Df.plot(kind='scatter',x = 'x',y='y')
#########
plt.show()
但它分别制作了两个图表,我想我的代码从numpy复制到数据帧是尴尬......
还有更好的解决方案吗?
答案 0 :(得分:2)
在ax
中使用轴句柄和df.plot
参数:
X1 = np.array([[1,2],[3,4]])
X2 = np.array([[2,3],[6,7]])
X1Df = pd.DataFrame(columns=['x','y'])
k = 0
for i in X1: # copy second numpy to DataFrome
temp = pd.DataFrame({'x' : i[0],
'y' : i[1]},index=[k])
k = k + 1
X1Df = pd.concat([X1Df,temp])
ax = X1Df.plot(kind='scatter',x = 'x',y='y')
X2Df = pd.DataFrame(columns=['x','y'])
k = 0
for i in X2: ## Copy second numpy to DataFrame
temp = pd.DataFrame({'x' : i[0],
'y' : i[1]},
index=[k])
k = k + 1
X2Df = pd.concat([X2Df,temp])
X2Df.plot(kind='scatter',x = 'x',y='y', ax=ax)
#########
plt.show()