In this post we will be creating a Python script that will generate a coloured line plot, to do this we will be using the plotnine package which is an implementation of a grammar of graphics in Python.
See the sample of Python code below where we utilize the plotnine package to generate a coloured line plot, for the purpose of this example we use pandas to create a DataFrame and generate dummy data for our X and Y axis. We then move on to create our coloured line plot using the geom_line parameters. To display the plot we use the ‘display’ function from the ‘IPython.display’ module.
In the example below we change our line colour to red.
import pandas as pd
from plotnine import *
from IPython.display import display
df = pd.DataFrame({'x': range(10), 'y': range(10)})
plot = (ggplot(df)
+ aes(x='x', y='y')
+ geom_line(color="red")
)
display(plot)
The above would return the following plot to output.

In another example below we change our line colour to blue and change to a dotted line type.
import pandas as pd
from plotnine import *
from IPython.display import display
df = pd.DataFrame({'x': range(10), 'y': range(10)})
plot = (ggplot(df)
+ aes(x='x', y='y')
+ geom_line(color="blue", linetype="dotted", size=2)
)
display(plot)
The above would return the following plot to output.

Take a look at some of our other content around the Python programming language by clicking here.