How to install ggplot2 in Python – Tutorial basic

In this tutorial, let’s see how to install ggplot2 in Python. ggplot2 is a popular data visualization package in R programming and has a strict implementation of the grammar of graphics which makes the plotting of data very consistent. We will use a Python package called plotnine that provides a grammar of graphics. If you have any experience using ggplot2 in R programming, then using ggplot2 in python using plotnine will be similar.

Installation

For this tutorial, we will use a google colab notebook with the plotnine package preinstalled. If you want to use your local environment or Jupyter Notebook, make sure you have python installed on your computer and install the required dependencies, like jupyter, pandas, etc., using pip.

The command to install plotnine is:

pip install plotnine

If you are using your local environment, the above command installs plotnine globally. But if you don’t want these packages to be installed globally, you can use python virtual environments to install these packages in a specific location locally.

Plotting using plotnine and ggplot2

Let’s import all the required dependencies inside the google colab notebook. We will use the diamonds dataset, which comes with the plotnine package, to see how ggplot2 works in python.

# importing dependencies
from plotnine import ggplot, aes, geom_point, labs
import pandas as pd
# importing dataset
from plotnine.data import diamonds

We will pick only the first 50 rows for simplicity.

diamonds = diamonds.head(50)

Let’s plot a graph now.

ggplot(diamonds) + aes(x='carat', y='price', color='cut')+ labs(
        x="carat",
        y="price (USD)",
        color="Name of vehicle",
        title="carat vs price",
    ) + geom_point()

Output:

install ggplot2 in Python

In the above code blocks, we imported ggplot, aes, labs, and geom_point from plotnine and used them. The names of the functions and usage are mostly similar to ggplot2 in R programming, which makes using it in python using plotnine identical to that of R.

Leave a Reply

Your email address will not be published. Required fields are marked *