[PYTHON] What I was worried about when displaying images with matplotlib

There were some points that I was a little worried about when displaying images with matplotlib, so I have summarized them. I'm sure there are people who are interested (especially those who are starting to use it), so I hope I can save the trouble of searching even a little. I use jupyter notebook a lot, so I assume that it will be used there.

1. 1. that? It's supposed to be grayscale, but it's colored?

seeing is believing. Let's display the image for the time being.


#The usual various imports
import cv2
import numpy as np
from matplotlib import pyplot as plt

im = np.array(cv2.imread('lena.png',0)) #Load the image and put it in numpy format

cv2.imread ('lena.png', 0) means to read "lena.png " to 0, that is, in grayscale. Now let's display the image with matplotlib.


#Display with matplotlib

Then it will be displayed like this.

スクリーンショット 2020-03-31 21.34.41.png

Somehow it is colored. Since matplotlib has a color map set by default, it looks like this. To release this, just insert plt.gray ().


plt.gray() #Color map"gray"Set to

スクリーンショット 2020-03-31 21.46.19.png It was as expected. Actually, there is information in various places so far, but I have thought this way.

But it felt good, so do you want to go back to the original color map?

I couldn't find it unexpectedly, and I thought, "I wonder if anyone wants to get it back ...". I didn't know what the default colormap was, so I gave up trying to restore it, but it was properly written in Official.

スクリーンショット 2020-03-31 21.54.38.png

This * viridis * seems to be the default colormap. Let's set it immediately.



スクリーンショット 2020-03-31 21.34.41.png Now it's back. Congratulations.

2. that? Is the contrast strange?

I think it was a miracle that I noticed this, but I thought that the color was a little strange. So, I tried to load such an image in the test. test.png It's Makuro. … But when it is displayed by matplotlib スクリーンショット 2020-03-31 22.20.06.png

It seems that the color gradation is normalized. In order to solve this, it seems that vmin and vmax must be specified as shown below.


plt.imshow(im, vmin = 0, vmax = 255) 

スクリーンショット 2020-03-31 22.27.35.png If you squint, you can see it slightly.

I'm sure there are other default settings that I'm not aware of. that's all. Please let me know if you have any mistakes.

Recommended Posts

What I was worried about when displaying images with matplotlib
What I was careful about when implementing Airflow with docker-compose
Where I was worried about heroku
I came across a lambda expression when I was worried about functionalization
I want to display multiple images with matplotlib.
Two things I was happy about with Python 3.9
What I was addicted to when using Python tornado
What I did when I was angry to put it in with the enable-shared option
What I was addicted to when migrating Processing users to Python
What I learned about Linux
What I was asked when using Random Forest in practice
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
I tried to summarize what was output with Qiita with Word cloud
A note I was addicted to when creating a table with SQLAlchemy
What was surprising about Python classes
When matplotlib doesn't work with python2.7
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
About launching an instance with an encrypted EBS volume (where I was addicted)
A reminder of what I got stuck when starting Atcoder with python
Addictive points when displaying logs with Tk.text
What I got into when using Tensorflow-gpu
What I referred to when studying tkinter
What I did with a Python array
What I was addicted to Python autorun
Log when I was worried that I could not connect to Wi-Fi on Linux
What I was addicted to when creating a web application in a windows environment
I failed when clustering with k-means, but what should I do (implementation of kernel k-means)
What I did when I got stuck in the time limit with lambda python
Three things I was addicted to when using Python and MySQL with Docker
When I tried to connect with SSH, I got a warning about free space.
A note I was addicted to when running Python with Visual Studio Code
[Linux] Let's talk about when I stumbled upon a symbolic link I was using.
A story that I was addicted to when I made SFTP communication with python