-1

I have the following codes:

For scatter 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')
plt.ticklabel_format(useOffset=False)
plt.yticks(np.arange(min(scaled_array[:,[0]]), max(scaled_array[:,[0]]), 0.05))
plt.legend()
plt.show()

This gives me: enter image description here

For line plot:

plt.plot(x, y)

This gives me:

enter image description here

I would like to plot both these two as subplots in the same plot (Vertically stacked plots).

I would like to know how can this be done

Thanks

Edit:

I tried doing:

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

And it gives me the desired plot as follows: enter image description here

But it also shows the following error:

TypeError: float() argument must be a string or a number, not 'PathCollection'
some_programmer
  • 3,268
  • 4
  • 24
  • 59
  • What is the problem right now you facing? Putting these commands next to each other will plot the line and the scatter in the same plot.Tell us what is not working – Sheldore Apr 29 '19 at 10:40
  • I don't want them in one single plot. I want them as 2 different plots in one subplot – some_programmer Apr 29 '19 at 10:41
  • You are using wrong terminology. What you actually need is a single figure with two subplots: One subplot for the line and the other subplot for the scatter. I am marking this as a duplicate. There are several answers on how to do this – Sheldore Apr 29 '19 at 10:44
  • Also, it is not clear what you mean by "one top of the other.". Does it mean upper figure with scatter and lower figure with line like a 2 row 1 column figure? "one top of the other." usually means scatter points on the top of line which means the same figure. Sometimes, it is better to include a sample hand sketched figure in your question in order of better explaining. – Sheldore Apr 29 '19 at 10:46
  • 1
    Try reading the [official](https://matplotlib.org/gallery/subplots_axes_and_figures/subplots_demo.html) examples – Sheldore Apr 29 '19 at 10:47
  • I will edit my question and include the graphs as well. – some_programmer Apr 29 '19 at 10:49
  • @Sheldore I have updated my question – some_programmer Apr 29 '19 at 11:03
  • 1
    This is explained in the official example I linked. Plz read them – Sheldore Apr 29 '19 at 11:06

1 Answers1

1

Try the following

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
Sheldore
  • 37,862
  • 7
  • 57
  • 71