Reputation: 5384
This is in the similar vein as Python Scatter Plot with Multiple Y values for each X ; that is, I have data which is:
data = [
[1, [15, 16, 17, 18, 19, 20]],
[2, [21, 22, 23, 24, 25, 26]],
[3, [27, 28, 29, 30, 31, 32]],
]
... so first column (0) is x-coordinates, and second column (1) contains arrays of y values corresponding to the single x coordinate. And, I want to plot this as a scatter plot, and the best I could do is this (code below):
Same as in the linked post, I've had to use three ax.scatter
plots, and hence we have three colours, one for each column.
So my question is:
ax.scatter
command to get a plot like the above (but with single color/marker) from the data I have (instead of having to issue three commands)?ax.scatter
command?Here is the code:
#!/usr/bin/env python3
import sys
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
print("versions: Python {} matplotlib {} numpy {}".format(sys.version.replace('\n', ''), matplotlib.__version__, np.version.version))
data = [
[1, [15, 16, 17, 18, 19, 20]],
[2, [21, 22, 23, 24, 25, 26]],
[3, [27, 28, 29, 30, 31, 32]],
]
ndata = np.asarray(data, dtype=object)
fig = plt.figure()
# Null formatter
ax = fig.add_subplot(1, 1, 1)
print()
print(ndata[1])
print(ndata[:,0].astype(float))
print(ndata[:,1])
datay_2D = np.stack(ndata[:,1], axis=0) # convert numpy array of lists to numpy 2D array
print()
print(datay_2D[:,0])
print(datay_2D[0])
print([ndata[:,0][0]]*len(datay_2D[0]))
ax.scatter([ndata[:,0][0]]*len(datay_2D[0]), datay_2D[0], marker="x")
ax.scatter([ndata[:,0][1]]*len(datay_2D[1]), datay_2D[1], marker="x")
ax.scatter([ndata[:,0][2]]*len(datay_2D[1]), datay_2D[2], marker="x")
plt.show()
Printout:
versions: Python 3.6.8 (default, Oct 7 2019, 12:59:55) [GCC 8.3.0] matplotlib 2.1.1 numpy 1.13.3
[2 list([21, 22, 23, 24, 25, 26])]
[ 1. 2. 3.]
[list([15, 16, 17, 18, 19, 20]) list([21, 22, 23, 24, 25, 26])
list([27, 28, 29, 30, 31, 32])]
[15 21 27]
[15 16 17 18 19 20]
[1, 1, 1, 1, 1, 1]
Upvotes: 2
Views: 3166
Reputation: 339130
I suppose all lists of y values have the same length? In that case
import numpy as np
import matplotlib.pyplot as plt
data = [
[1, [15, 16, 17, 18, 19, 20]],
[2, [21, 22, 23, 24, 25, 26]],
[3, [27, 28, 29, 30, 31, 32]],
]
x, y = zip(*data)
y = np.array(y)
plt.scatter(np.repeat(x, y.shape[1]), y.flat)
plt.show()
Upvotes: 4