You are on page 1of 14

Matplotlib Visualization with Python - color

Filip Andrić
Zagreb School of Economics and Management
TABLE OF CONTENT

Contents
Introduction........................................................................................................................................................................... 3
Basic codes to start with in python.............................................................................................................................4
Matplotlib.pyplot.colors() - This function is used to specify the color. It is do-nothing
function............................................................................................................................................................................... 4
List of named colors with python.................................................................................................................................8
Specifying colors.................................................................................................................................................................. 9
Visualizing the Color Spaces of Images with Python and MatPlotLib.......................................................11
References........................................................................................................................................................................... 14
Introduction

Matplotlib is a comprehensive library for creating static, animated, and interactive


visualizations in Python. Matplotlib makes easy things easy and hard things possible.

● Create publication quality plots.


● Make interactive figures that can zoom, pan, update.
● Customize visual style and layout.
● Export to many file formats .
● Embed in JupyterLab and Graphical User Interfaces.
● Use a rich array of third-party packages built on Matplotlib.

A large number of third party packages extend and build on Matplotlib functionality,
including several higher-level plotting interfaces (seaborn, HoloViews, ggplot, ...), and a
projection and mapping toolkit (Cartopy).

Hist2d(x,y)
Basic codes to start with in python

In Python we can plot graphs for visualization using Matplotlib library. For integrating plots into
applications, Matplotlib provides an API. Matplotlib has a module named pyplot which provides
a MATLAB-like interface.

Matplotlib.pyplot.colors() - This function is used to specify the color. It is do-nothing function


We can use this function for various data visualizations and obtain insights from them.
Here, marker=’o’ represents circle while markerfacecolor is used for specifying color of the
point marker.
List of named colors with python
Specifying colors
Matplotlib recognizes the following formats to specify a color.
"Red", "Green", and "Blue" are the intensities of those colors. In combination, they represent
the colorspace.

Matplotlib draws Artists based on the zorder parameter. If there are no specified values,
Matplotlib defaults to the order of the Artists added to the Axes.

The alpha for an Artist controls opacity. It indicates how the RGB color of the new Artist
combines with RGB colors already on the Axes.

The two Artists combine with alpha compositing. Matplotlib uses the equation below to
compute the result of blending a new Artist.
Visualizing the Color Spaces of Images with Python and
MatPlotLib

They say a picture is worth a thousand words, but that also means that storing an image takes
up considerably more storage than a word. As such, a great deal of research has been made to
understand the composition of images and how these compositions can be represented
efficiently. In this project, we will not be talking about image compression, but we will be
breaking down the composition of images in a visually appealing way

Specifically, we are going to be using Python to break down the colors that are present in an
image and we are going to be visualizing this color space using MatPlotLib.

First, let’s talk about how we can represent a color in a 3D space. Software tends to break down
colors into an RGB value: The mixture of Red, Green, and Blue values that compose the color.

Now, these red, green, and blue values typically range from 0 to 255. Since each color is
therefore represented by 3 values ranging from 0 to 255, we can plot any color on a 3D space
fairly easily. For example, here are a few of the basic colors plotted on a 3D space.
In our example, the red value of a color determines its x coordinate, green determines its y
coordinate, and blue determines its z coordinate. As you can see, when x and z are both 255
then we get violet, and when all 3 color parameters are 0 we get solid black. We can represent
any color in the RGB color model through x, y, and z coordinates.
Alright, now let's start actually working with python. First, we are going to install our necessary
libraries. Run the following command to install MatPlotLib, the library we will be using to plot
our colors. Next, we need to install Pillow, a Python library that lets us decompose images. Now
let’s import everything we need at the top of our python file: Now let’s load in the image you
want to breakdown with Pillow. For this example, I’m going to be using this nice photo:

im.load() returns us a 2D array storing each pixel. Each pixel object is an array of 3 integers

representing the RGB values of that specific pixel. I recommend that you play around with this

px value to best understand how it is storing the pixels.Next, let's set up our 3D space.

MatPlotLib makes this incredibly easy. First, we are going to create the axes of our graph which

we will specify to be 3D. Note that MatPlotLib requires that we normalize the RGB values

between 0 and 1 when providing colors, so we divide each RGB value by 255. Also, note that this

code will often result in the arrays containing duplicates, which is going to make MatPlotLib do

more work than necessary. To stop this, we can add an if statement so that it only adds the color

if it is not already in the array.

We get the following plot:


References
•https://matplotlib.org/stable/gallery/index.html

•https://matplotlib.org/3.0.0/gallery/color/named_colors.html

•https://matplotlib.org/stable/gallery/shapes_and_collections/collections.html

•https://www.geeksforgeeks.org/matplotlib-pyplot-colors-in-python/

•https://liorben-david.medium.com/visualizing-the-color-spaces-of-images-with-

python-and-matplotlib-79af1577e6cc
Note: This function has been deprecated since version 2.1.

You might also like