I tried to find something I can only do with plt.scatter, that plt.plot can't, but with the use of the Line2D args and everything else it seems to me plt.scatter is basically useless. I came across this quandary after I tried to write a script for automated plotting with some user specified preferences. At first it seemed as if I would have to use both plot() and scatter() with an if condition, if the user wants only a marker, only lines or both. But that isn't necessary at all. I know this is sort of an open ended question, but shouldn't scatter be considered a deprecated function at this point? All it does is having a different default plotting mode. It I don't want a line, I can just set linewidth=0, right?
Asked
Active
Viewed 462 times
1 Answers
3
Yes
If you want a set of single-sized, single-colored markers, yes, you can use plt.plot(x,y, linestyle="none")
instead of plt.scatter(x,y)
. plt.plot
is also a bit more efficient in such a case.
No
However, if you want to encode information into the size or the color of a set of markers, you will want to use plt.scatter(x, y, s=sizes, c=colors)
. For example:
import numpy as np
import matplotlib.pyplot as plt
x, y, c, s = np.random.rand(4, 40)
s = s * 100 + 5
fig, ax = plt.subplots()
sc = ax.scatter(x, y, s=s, c=c)
fig.colorbar(sc)
ax.legend(*sc.legend_elements("sizes"), loc="upper left")
plt.show()

Community
- 1
- 1

ImportanceOfBeingErnest
- 321,279
- 53
- 665
- 712
-
So the big difference is that color and sizes can be containers in scatter but only a single value for all datapoints in plot. Thank you. But wouldn't it be quite simple to add the functionalities of scatter to plot and make them one function? I feel this could be very convenient in a lot of cases... – J.Doe Aug 20 '19 at 15:59
-
2No, it's not simple; already now `plot` as well as `scatter` are quite complex internally. But the main reason they are separate is that they create totally different objects; and it actually makes sense to have them create different objects because drawing a line should be much more efficient. – ImportanceOfBeingErnest Aug 20 '19 at 16:36