Open In App

An Introduction to Grammar of Graphics for Python

A grammar of graphics is basically a tool that enables us to describe the components of a given graphic. Basically, what this allows us to see beyond the named graphics, (scatter plot, to name one) and to basically see the underlying statistics behind it. The grammar of graphics was originally introduced by Leland Wilkinson in the 1990s and was popularized by Hadley Wickham with ggplot.

Components of Grammar of graphics

Typically, to build or describe any visualization with one or more dimensions, we can use the components as follows. 



This grammar of graphics was first introduced in R, using ggplot and ggplot2. Considering its success in the past, it is also been introduced in Python as plotnine. 

Python binding

plotnine is an implementation/binding of a grammar of graphics in Python. It is based on ggplot2. So, basically, if you’re familiar with R programming and ggplot2, chances are that you would catch up with plotnine in almost no time. There are only 2 noticeable changes in ggplot2 and plotnine. 



Installation

This module does not come built-in with Python. To install this module type the below command in the terminal.  

pip install plotnine 

Example 1:  




import pandas as pd
from plotnine import * 
  
  
# load dataset 
dataset = pd.read_csv("dataset.csv"
  
# ggplot is to plot the given data
(ggplot(dataset, aes(x = "area_0", y = "area_1"))+
    geom_point()
)
  
# aes contains parameters which work 
# as x-axis and y-axis for the given plot
# geom.point() makes the data entries as points

Output: 

Example 2: 




import pandas as pd
from plotnine import * 
  
  
# load dataset 
dataset = pd.read_csv("dataset.csv"
  
(ggplot(dataset, aes(x = "area_0", y = "area_1"))+
    geom_point(color = "label", alpha = 0.7,
               size = 0.5)
)

Output: 

 


Article Tags :