Home > Back-end >  How to scatter plot each group of a pandas DataFrame
How to scatter plot each group of a pandas DataFrame

Time:11-18

I am making a scatter plot with the geyser dataset from seaborn. I am coloring the points based on the 'kind' column but for some reason, the legend only shows 'long' but leaves out 'short'. I don't know what I am missing. I also was wondering if there is a simpler way to color code the data one that does not use a for-loop. Thanks!

x = geyser_df['waiting']
y = geyser_df['duration']
col = []

for i in range(len(geyser_df)):
    if (geyser_df['kind'][i] == 'short'):
        col.append('MediumVioletRed')
    elif(geyser_df['kind'][i] == 'long'):
        col.append('Navy')

plt.scatter(x, y, c=col)
plt.legend(('long','short'))
plt.xlabel('Waiting')
plt.ylabel("Duration")
plt.suptitle("Waiting vs Duration")
plt.show()

enter image description here

CodePudding user response:

  • The correct way to do this with pandas is with enter image description here

    • The easiest way is with seaborn, a high-level API for matplotlib, where hue is used to separate groups by color.
      • figure

  • Related