Search code examples
pythonpandasmatplotlibscatter

Is there a simple way to plot multiple series on one pandas scatter plot?


I come across this issue constantly; and my current solution is to create additional dataframes, I feel like there must be an easier solution.

Here is an example of data where I have multiple countries with multiple attributes: enter image description here

If I wanted to plot Population vs. Depression (%) I would write:

ax = df.plot.scatter(x='Population', y='Depression (%)') enter image description here

This isn't super helpful, as there are clearly lines linked to specific Countries (df['Country']). Is there a simple way to plot a scatter plot with different series (colors/shapes/etc) as different Countries?

Right now I use groupby to separate out individual Countries and plot them on the same axes (ax = ax).

Any thoughts or input would be greatly appreciated! Thank you!


Solution

  • Try c="Country" and then if you want some nice colors you can go colormap='viridis' for example documentation

    ax2 = df.plot.scatter(x='length',
                          y='width',
                          c='species',
                          colormap='viridis')
    

    enter image description here

    Since you are using strings as variables we can't use this approach directly and need to convert the data to numbers. This can be done by writing: c=df.country.astype("category").cat.codes