我有以下代码:
散点图:
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'
答案 0 :(得分: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