# 如何在一个图中绘制散点图和线图作为子图？

##### 问题描述投票：-1回答：1

``````plt.scatter(x_1.values[y_ocsvm1 == 1, 2], scaled_array[y_ocsvm1 == 1, 0], c = 'red', label = 'cluster1')
plt.scatter(x_1.values[y_ocsvm1 == -1, 2], scaled_array[y_ocsvm1 == -1, 0], c = 'blue', label = 'cluster2')
plt.ticklabel_format(useOffset=False)
plt.yticks(np.arange(min(scaled_array[:,[0]]), max(scaled_array[:,[0]]), 0.05))
plt.legend()
plt.show()
``````

``````plt.plot(x, y)
``````

``````fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
ax1.plot(x, y)
ax2.plot(plt.scatter(x_1.values[y_ocsvm1 == 1, 2], scaled_array[y_ocsvm1 == 1, 0], c = 'red', label = 'cluster1')
, plt.scatter(x_1.values[y_ocsvm1 == -1, 2], scaled_array[y_ocsvm1 == -1, 0], c = 'blue', label = 'cluster2'))
``````

``````TypeError: float() argument must be a string or a number, not 'PathCollection'
``````
python python-3.x matplotlib seaborn
##### 1个回答
1

``````fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)

ax1.plot(x, y)

ax2.scatter(x_1.values[y_ocsvm1 == 1, 2], scaled_array[y_ocsvm1 == 1, 0],
color='red', label='cluster1')
ax2.scatter(x_1.values[y_ocsvm1 == -1, 2], scaled_array[y_ocsvm1 == -1, 0],
color='blue', label='cluster2')

plt.legend() # To show the legend
``````