Scatter plots with different colors depending on flower species

Hi guys! :smiley:

I am new to ggplot2 and would appreciate a little help please :slight_smile:

I am playing around with famous IRIS flower dataset.
This code is working great in Python 3 script.

c_col = ["#2f4858", "#f6ae2d", "#f26419",
         "#33658a", "#55dde0", "#2f4858",
         "#2f4858", "#f6ae2d", "#f26419",
         "#33658a", "#55dde0", "#2f4858"]

fig = (
    ggplot(data.dropna(subset = ['sepal_length'])) +
        aes(x = 'sepal_length',
            y = 'sepal_width'),
        fill = c_col[10], color = 'blue'
    ) +
        title ='Relationship between sepal_length and sepal_width',
        x = 'sepal_length',
        y = 'sepal_width',

I was wondering how to add a feature to plot three different colors depending on flower species... above code plots all three flower species in BLUE color... but I would like to have three colors in my Scatered diagram... so that every flower specie is in its own color.

Waiting your reply!
Many, many thanks!

Ivan :smiley:

Here is an example based on the iris dataset (you appear to have something based on it):

ggplot(iris) + 
    aes(x = Sepal.Length,
        y = Sepal.Width, 
        colour = Species)
  ) +
    title ='Relationship between sepal_length and sepal_width',
    x = 'sepal_length',
    y = 'sepal_width'

Thanks Martin.

I tried above code, but does not work from Python script.

I am using 'plotnine' pip lib to be able to run ggplot2 from Python script.

So syntax is a little different.

Ok, I didn't read the Python bit.

Assuming it works the same as R's ggplot2, then just move the colour bit into the aes() function to vary the species by colour. You'll also want scale_colour_manual() to pick from your list of colours..

If you're using the Python implementation of ggplot2 (ggplot) in Python, you might have better luck asking on Stack Overflow or a Python forum, if no one has the answers for you here.

There's a notebook full of examples in the repo here:


Hi Martin... does not work.

I will play around with an additional "key-value" R lib... to see what I can achieve :slight_smile:


Hi Martin,

I found solution here that works, provided by Dr. Gregor Scheithauer:

Data visualization in Python

data['ounces_str'] = data['ounces']
data['ounces_str'] = data['ounces_str'].apply(str)
fig = (
    ggplot(data.dropna(subset = ['abv'])) +
        aes(x = 'abv',
            y = 'ibu',
            fill = 'ounces_str'),
        alpha = 0.5,
        color = 'black'
    ) +
        title ='Relationship between alcoholic content (abv) and int. bittering untis (ibu)',
        x = 'abv - The alcoholic content by volume',
        y = 'ibu - International bittering units',
    ) +
        name = 'Ounces',
        values = c_col) +
        limits = (0, 0.14),
        labels = labels(0, 0.14, 0.02),
        breaks = breaks(0, 0.14, 0.02)
    )  +
        limits = (0, 150),
        labels = labels(0, 150, 30),
        breaks = breaks(0, 150, 30)

Thank you for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.