How to Plot Images with Plotly Imshow
This article is originally published at https://www.sharpsightlabs.com
In this tutorial, I’ll show you how to use the Plotly imshow function to display images in Python.
So I’ll explain the syntax of
px.imshow. I’ll also show you a clear, step-by-step example of how to display an image with Plotly.
The tutorial has a few sections, and if you need something specific, just click on one of the following links.
Table of Contents:
Ok. Let’s get to it.
A Quick Introduction to the Plotly imshow Function
The Plotly imshow function has multiple uses.
For example, you can use the function to plot heatmaps and to plot numbers visually as colors.
But one of the best ways to use the function is to display images. (As the name implies, its fundamental purpose is for “image show”.)
So you can use Plotly imshow to visualize images as part of an image processing workflow.
In this tutorial, we’ll strictly stick to plotting images like .jpg and .png files, and we’ll cover heatmaps and other techniques in a separate tutorial.
The syntax of px.imshow
Now that I’ve given you a quick overview of what the Plotly imshow function does, let’s take a look at the syntax.
The syntax of px.imshow is very simple.
Typically, we call the function as
Remember: this syntax assumes that you’ve imported Plotly express with the nickname
px. You can do that with the code
import plotly.express as px.
Input to px.imshow
Let’s talk quickly about the input to the function.
In the syntax explanation above, you’ll notice the input that I’ve called
your_image. This is often an .jpg or a .png file.
However, typically, before you can plot the image with px.imshow, you need to read it in with a separate function. Specifically, you need to read in the image with a function like the
imread() function from scikit-image. I’ll show you how to do this in the examples section.
Examples: how to visualize images with Plotly Express
Now that we’ve looked at the syntax, let’s look at some examples of how to plot images and pictures with Plotly Express.
Run this code first
Before you run the examples, you’ll need to import some packages and run some preliminary code.
First we need to import a few Python packages.
import plotly.express as px import skimage.io as skio
We obviously need
plotly.express to run the
We also need
skimage to import our image and read it into our Python environment.
If you don’t have those packages, then you’ll need to install them first. Typically, these days, I install all of my Python packages via Anaconda. You can get more information here about installing skimage via Anaconda. You can get more information about installing Plotly here.
Set Up Image Rendering
By default, Plotly is set up to render images in browser windows.
So, if you’re using an Integrated Development Environment like Spyder to do your data science work, you’ll need to set it up to render plots from Plotly. (I use Spyder, so I need to do this step myself.)
Note: if you’re using Jupyter, you can skip this code!
To set up Plotly to render your plots as svg images in your IDE, you can run the following code:
import plotly.io as pio pio.renderers.default = 'svg'
Get Image (i.e., read in the image with scikit-image)
Next, let’s get an image that we can plot.
The image is in a folder online at sharpsightlabs.com, and we’ll use the
imread() function from
skimage to read it in.
ron = skio.imread('https://www.sharpsightlabs.com/datasets/images/ron-smirk.png')
Once you run that code, you should be ready to go.
(If you have any problems getting the image, leave a comment in the comments section near the bottom of the page.)
EXAMPLE 1: Plot an image with px.imshow
Here, we’re going to “plot” the image with the Plotly imshow function.
This is fairly straightforward.
Here’s the code:
And here’s the output:
Ok. This is pretty simple.
We called the Plotly imshow function with the syntax
Inside the parenthesis, we’ve passed in an image. The image is the “ron-smirk.png” file. The file is being stored with the variable name
As you can see from the file ending, this is a
.png file. But you could also use a
.jpg file, and several other image types should work.
EXAMPLE 2: Remove the tick labels from the x and y axes
Now, we’ll remove the tick labels from the x-axis and y-axis.
In example 1, you probably noticed the numbers along the x and y axes. These are the “tick” labels of the axes.
For some applications, it may be fine to include them, but in some cases you may want to remove them.
To remove them, you need to use the
update_xaxes() method and the
update_yaxes() method. Importantly, inside the parenthesis, we need to set the
showticklabels parameter to
showticklabels = False.
Let’s take a look:
(px.imshow(ron) .update_xaxes(showticklabels = False) .update_yaxes(showticklabels = False) )
Obviously, the output here is almost exactly the same as the output for example 1.
The only difference here is that we’ve removed the axis ticks (i.e., the numbers along the axis) by using
Notice as well that I used a special technique to call these methods. I’ve “chained” these methods together, one after another, but they’re on multiple lines.
To do this, the whole expression is enclosed inside parenthesis.
This is very similar to the “secret” pandas chaining syntax, which allows you to chain together multiple pandas methods to do advanced data manipulation in a single block of code.
I strongly recommend that you learn this technique to chain together Python methods. It’s very powerful once you learn how to use it properly.
Leave your other questions in the comments below
Do you still have questions about plotting images with the Plotly imshow function?
If so, just leave your questions in the comments section below.
If you want to master Plotly, join our course
In this blog post, I’ve shown you how plot an image in Python using the Plotly imshow function (AKA
px.imshow). But to really master Python data visualization with Plotly, there’s a lot more to learn.
That said, if you’re serious about learning Plotly and if you want to master data visualization in Python, you should join our premium online course, Plotly Mastery.
Plotly Mastery is an online course that will teach you everything you need to know about Python data visualization with the Plotly package.
Inside the course, you’ll learn:
- how to create essential plots like bar charts, line charts, scatterplots, and more
- techniques for creating multivariate data visualization
- how to add titles and annotations to your plots
- learn “how to think” about visualization
- how to “customize” your charts, and make them look beautiful
- how to “tell stories with data”
- and much more …
Additionally, when you enroll, you’ll get access to our unique practice system that will enable you to memorize all of the syntax you learn. If you practice like we show you, you’ll memorize Plotly syntax and become “fluent” in data visualization.
You can find out more here:
Please visit source website for post related comments.