# Matplotlib

## How to Add a Second x-axis in Python Matplotlib?

Problem Formulation Given some data. How to plot the data in the same 2D plot but using two x-axes instead of only one? You want to have on top axis and one bottom axis. You want to be able to customize the ticks and labels of both the top and bottom axis. We use Matplotlib …

## Smoothing Your Data with the Savitzky-Golay Filter and Python

This article deals with signal processing. More precisely, it shows how to smooth a data set that presents some fluctuations, in order to obtain a resulting signal that is more understandable and easier to be analyzed. In order to smooth a data set, we need to use a filter, i.e. a mathematical procedure that allows …

## Matplotlib Text and Annotate — A Simple Guide

You’d like to add text to your plot, perhaps to explain an outlier or label points. Matplotlib‘s text method allows you to add text as specified coordinates. But if you want the text to refer to a particular point, but you don’t want the text centered on that point? Often you’ll want the text slightly …

## How To Plot SKLearn Confusion Matrix With Labels?

Summary: The best way to plot a Confusion Matrix with labels, is to use the ConfusionMatrixDisplay object from the sklearn.metrics module. Another simple and elegant way is to use the seaborn.heatmap() function. Note: All the solutions provided below have been verified using Python 3.9.0b5. Problem Formulation Imagine the following lists of Actual and Predicted values …

## How to Set the y-Axis Limit in Python Matplotlib

If you work in the field of data science you might have to draw a lot of plots using either Matplotlib or Seaborn. In this blog post, you will learn how to set a limit to the y-axis values in Matplotlib. We will start by loading the Boston household data and process the data to …

## How to Display an Image as Grayscale in Python Matplotlib?

How to Display an Image as Grayscale in Python Matplotlib? You can convert a given image to a grayscale image using four simple steps: Import the PIL and Matplotlib libraries Open the image with PIL.Image.open(filename). Convert the opened image to grayscale using img.convert(“L”) with greyscale mode “L”. Display the image using Matplotlib’s plt.imshow(gray_img, cmap=’gray’) function. …

## How to Display, Modify and Save Images in Matplotlib

This article will show you how to visualize, modify and save images in Python, more specifically in Matplotlib. In the following sections, we will see how to load images, how to modify some of their properties and finally how to save them. Long story short If you want to import an image and to display …

## Python OpenCV Image Processing – Resize, Blend, Blur, Threshold, Convert

This tutorial is an introduction to the OpenCV library. Learn how to convert color channels, resize, blend, blur, and threshold images in Python. The OpenCV  library contains most of the functions we need for working with images. Handling images in programming requires a different intuition than handling text data. An image is made up …

## Matplotlib – How to Change Subplot Sizes

How to change the size of your matplotlib subplots? The method .subplots() can easily generate multiple subplots within the same plotting figure. But as you may have already noticed, all the subplots have the exact same size and moreover, their size will be adjusted automatically, depending on how many of them you want to display …

## How to Extract Text from Images in Python using OpenCV and EasyOCR

You can extract text from images with EasyOCR, a deep learning-based OCR tool in Python. EasyOCR performs very well on invoices, handwriting, car plates, and public signs. First released in 2007, PyTesseract  is the to-go library for extracting text from images. It uses classical computer vision methods to perform optical character recognition (OCR), then …

## How to Customize Multiple Subplots in Matplotlib

This article will cover a specific issue that most Python users encounter when using Matplotlib for plotting their graphs. I am talking about defining multiple subplots and being able to access and change their properties individually. In the following sections, we will see how to use the Matplotlib function .subplots() for generating multiple subplots and …

## How to Plot the Confidence Interval in Python?

Problem Formulation: How to plot the confidence interval in Python? To plot a filled interval with the width ci and interval boundaries from y-ci to y+ci around function values y, use the plt.fill_between(x, (y-ci), (y+ci), color=’blue’, alpha=0.1) function call on the Matplotlib plt module. The first argument x defines the x values of the filled …