Editing Plots in Python

A comprehensive list of how to customize your plots using Matplotlib and Seaborn for beginners

Preeya Sawadmanod
Analytics Vidhya
4 min readOct 8, 2019

--

As someone who recently learned how to plot graphs in Python, I have spent a lot of time googling things of how to edit my visualizations. Adding titles, changing x and y labels or even changing figure sizes can be a tedious task as there are so many ways of plotting using Python. Since this is a beginner’s guide, I am going to talk about two different methods of visualization in Python (Matplotlib and Seaborn) and how to edit and clean plots within these methods.

To show and explain differences between Matplotlib and Seaborn, I am going to use the data set iris from sklearn to demonstrate some plots. This data sets consists of 3 different types of irises’ (Setosa, Versicolour, and Virginica) petal and sepal length and width. I am going to skip the loading and cleaning part of the data and get right into the plotting.

Plotting using both methods
(Left plot = Matplotlib and Right plot = Seaborn )

We all have our preferred method when it comes to plotting. Sometimes I use Seaborn and other times I use Matplotlib. Unfortunately, I spend most of my time editing my labels, title, size and etc in my plots. For this example: Matplotlib only shows the y-label and Seaborn shows only the x-label without any further inputs. This is usually the time when I start googling: “Add x-label Matplotlib” or ”Add y-label Seaborn”.

To save some of your time I have created a few codes below, that might solve your problem when it comes to cleaning your plots.

Single plots

Matplotlib & Seaborn

Matplotlib in pink and Seaborn in Blue
code block for both plots

Because seaborn is very compatible with matplotlib, we can basically use the same codes to edit both of our plots. Below you can find a list of my common edits when plotting.

List of common edits

Click on Github to search through the list

However, plotting only one plot is easy and very fast to customize, but what if you want to plot multiple subplots? This is where I would recommend using Subplots or Pairplot.

Multiple Plots

Pairplot

By default, Pairplot will create a grid of axes such that each variable in data will by shared in the y-axis across a single row and in the x-axis across a single column. The diagonal axes draws a plot to show the distribution of the data for the variable in that column.

Plotting Pairplot using Seaborn and coloring “species”

I would recommend using Pairplot only if there are not many features you are trying to plot. However, as you can see, we can’t really customize these plots, and there are some plots we actually do not need.

There is a way to solve this problem by specifying your variables you would like to plot (see code block below).

Plotting scatter plots using Pairplot

Subplots

Another way to create multiple plots is using subplots. When plotting multiple subplots, where you would like to customize your edits, I would recommend using a function. This saves you a lot of time in editing your plots.

Plotting subplots using a function
Scatter subplots function code

Conclusion

There are so many ways how you can edit and customize your plots. Matplotlib and Seaborn are two great starting tools, when you are a beginner in Python and are trying to explore data visualization.

--

--

Preeya Sawadmanod
Analytics Vidhya

I am an enthusiastic and motivated Data Scientist who is passionate about machine learning solutions to solve trending issues.