I have a dataframe with 3 columns. I would like to plot col1 on the x axis with col2 and col3 on the y axis. Col1 has repeating values, so for each x value there are duplicate y values.
Example dataframe:
DF = pd.DataFrame({"name": ["Alice", "Alice", "Charles", "Charles", "Kumar", "Kumar"],
"height": [124, 126, 169, 170, 175, 174],
"weight": [100, 105, 123, 125, 139, 140]})
DF
name height weight
0 Alice 124 100
1 Alice 126 105
2 Charles 169 123
3 Charles 170 125
4 Kumar 175 139
5 Kumar 174 140
I want:
A) each person to occur only once on the x axis
B) keep all heights one color and all weights another color, with an accurate, non-repeating legend
So far I can get either A or B, not both. Below is what I'm trying and the output. For A, this was helpful (Python Scatter Plot with Multiple Y values for each X)
For A:
f = DF.groupby("name", as_index=False).agg({"height":lambda x: tuple(x), "weight":lambda x: tuple(x)})
for x, (y1, y2) in enumerate(zip(f.height.values.tolist(), f.weight.values.tolist()), start=1):
plt.scatter([x] * len(y1), y1, color='green', marker='o', label="height")
plt.scatter([x] * len(y2), y2, color='blue', marker='o', label="weight")
plt.xticks(np.arange(1, len(f.name.values) +1))
plt.axes().set_xticklabels(f.name.values.tolist())
plt.legend(loc="best")
plt.show()
For B:
ax = DF.plot(style="o", figsize=(7, 5), xlim=(-1, 6))
ax.set_xticks(DF.index)
ax.set_xticklabels(DF.name, rotation=90)
plt.show()