How to change background color in ggplot2 Python
In this tutorial, we are going to learn how to change the background color in the plots made using ggplot2 in Python. ggplot2 is a popular data visualization package in R programming, but we can use it in python using the plotnine package. The plotnine package provides us with the grammar of graphics just like ggplot2 in R, and using it is also very similar to ggplot2 in R.
Installation and Importing
For this tutorial, we will use a google colab notebook which has the plotnine package preinstalled.
We suggest you check this once if you are new to ggplot2 with Python: ggplot2 installation guide
We will also use the diamonds dataset from the plotnine package.
If you are using jupyter notebook or your local environment, then install dependencies using the following commands.
pip install plotnine pip install pandas
Let’s import all the required packages and files.
# importing dependencies from plotnine import ggplot, aes, geom_point, labs, theme, element_rect, theme_light, element_line import pandas as pd # importing dataset from plotnine.data import diamonds
Here from plotnine
, I am importing the diamonds dataset and required ggplot2 functions.
Changing background color in ggplot2 in Python
Let’s pick only the first 40 rows of the dataset for simplicity.
diamonds = diamonds.head(40)
We can change the panel background or the plot background. Let’s see how to do both of them.
Changing plot background
We can use the plot_background
argument inside the theme()
method to change the background color of the plot. Here we use the element_rect()
method to fill the background. The fill and color arguments in the element_rect()
method change the color of the background and border of the plot. We can also use hexadecimal color values instead of using the color names.
ggplot(diamonds) + aes(x='carat', y='price', color='cut')+ labs( x="carat", y="price (USD)", color="Cut type", title="carat vs price", ) + geom_point() + theme(plot_background= element_rect(fill='lightblue', color='lightblue'))
In the above code, aes()
is used to set the aesthetics of the graph, labs()
to set the labels, geom_point()
to plot a scatter plot, and theme()
to customize the non-data components of the graph.
Output:
Changing panel background
We can use the panel_background
argument inside the theme()
method to change the background color of the panel in the plot.
ggplot(diamonds) + aes(x='carat', y='price', color='cut')+ labs( x="carat", y="price (USD)", color="Cut type", title="carat vs price", ) + geom_point() + theme(panel_background= element_rect(fill='lightblue', color='lightblue'))
Output:
We can also change the minor and major grid line’s color.
ggplot(diamonds) + aes(x='carat', y='price', color='cut')+ labs( x="carat", y="price (USD)", color="Cut type", title="carat vs price", ) + geom_point() + theme( panel_background = element_rect(fill = "lightblue", color = "lightblue", size = 0.5, linetype = "solid"), panel_grid_major = element_line(size = 0.5, linetype = 'solid', color = "#000000"), panel_grid_minor = element_line(size = 0.25, linetype = 'solid', color = "#000000") )
Output:
Using inbuilt themes to change the theme of the entire plot
ggplot2 has several inbuilt themes that we can use to change the entire visuals of the graph, including the background color.
ggplot(diamonds) + aes(x='carat', y='price', color='cut')+ labs( x="carat", y="price (USD)", color="Cut type", title="carat vs price", ) + geom_point() + theme_light()
Output:
All the inbuilt themes are listed below:
- theme_gray()
- theme_bw()
- theme_linedraw()
- theme_light()
- theme_dark()
- theme_minimal()
- theme_classic()
- theme_void()
- theme_test()
Leave a Reply