Reader small image

You're reading from  Matplotlib for Python Developers. - Second Edition

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788625173
Edition2nd Edition
Languages
Right arrow
Authors (3):
Aldrin Yim
Aldrin Yim
author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim

Claire Chung
Claire Chung
author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

Allen Yu
Allen Yu
author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu

View More author details
Right arrow

Chapter 3. Decorating Graphs with Plot Styles and Types

In the previous chapter, we learned some basic concepts to draw line and scatter plots with Matplotlib, and made adjustments to a few elements. Now that we are familiar with the Matplotlib syntax, we are ready to go further and explore the potential of Matplotlib.

In this chapter, we will discuss:

  • Color specification
  • Line style customization
  • Point style customization
  • More native plot types
  • Inserting text and other annotations
  • Considerations in plot styling

Controlling the colors


Color is an essential element in any visual. It can have a huge impact on how graphics are perceived. For example, sharp color contrast can be used to highlight a focus; a combination of several distinct colors is useful in setting up a hierarchy.

In Matplotlib 2, colors have been set by default to better differentiate between categories; and to perceive continuous numerical values more intuitively, yet we often need better control over the colors to represent our data. In this section, we will introduce the common color options in Matplotlib.

Default color cycle

A color cycle is a list of colors used to control the color of a series of elements automatically, such as each data series in multiline plots. In Matplotlib 2.0, the default color cycle has expanded from 7 to 10 colors using the category10 palette in Data-Driven Documents (D3) https://github.com/d3 and Vega, a declarative language for visualization grammar. These colors are designed to show good contrast between...

Line and marker styles


We have demonstrated how to draw line plots and scatter plots in the previous chapter. We know that scatter plots are made up of dots denoting each data point, whereas line plots are generated by joining dots of data points. In Matplotlib, the marker to mark the location of data points can be customized to have different styles, including shape, size, color, and transparency. Similarly, the line segments joining the data points as well as different 2D lines that share the same class in the object-oriented Matplotlib structure can have their styles adjusted, as briefly demonstrated in the grid section of the previous chapter. Adjusting marker and line styles is useful in making the data series more distinguishable, and sometimes for aesthetic considerations. In this section, we will go through the details and implementation methods of marker and line styles in Matplotlib.

Marker styles

For markers denoting data points, we can adjust their shapes, sizes, and colors. By...

More native Matplotlib plot types


Besides the most basic scatter and line plots, Matplotlib provides a versatile collection of plot types to serve different data visualization purposes. In this section, we will introduce the rationale of plot type selection and the usage of each type.

Choosing the right plot

A successful visualization must communicate the message well. In order to achieve this goal, we need to have a good understanding of the nature of our data as well as the advantages and limitations of each plot type in illustrating different relationships in data. In choosing the right plot type to display, we have the following considerations:

  • Number of variables
  • Distribution of data
  • Relationships between data series

Histogram

Histograms are useful in surveying the distribution of data. For example, we can plot data on a histogram when we want to see some age groups distributed in a population, light exposure in a photograph, or the amount of precipitation in each month in a city.

In Matplotlib...

Text and annotations


To enhance the understanding of plot details, we may sometimes add in text annotations for explanation. We will now introduce the methods of adding and adjusting text in Matplotlib plots.

Adding text annotations

We can add text to our plot by calling plt.text(x,y,text); we specify the x and y coordinates and the text string. Here is a quick example:

plt.text(0.25,0.5,'Hello World!',fontsize=30)
plt.show()

You can see in this figure the Hello World! message appearing in the center of the plot:

Font

Here are some of the common font properties adjustable in Matplotlib:

  • Font size: Float or relative size, for example, smaller and x-large
  • Font weight: For example, bold or semibold
  • Font style: For example, italic
  • Font family: For example, Arial
  • Rotation: Angle in degrees; it is vertical or horizontal

Note

Matplotlib now supports unicode and emoji.

Mathematical notations

As a plotting tool, mathematical notations are common. We can use the in-built mathtext or LaTeX to render mathematical...

Using style sheets


We have learned to style our plots step by step so far. For more persistent and portable settings, we can apply a predefined global style via the matplotlib.style module:

## Available styles
 Matplotlib provides a number of pre-built style sheets. You can check them out by with `matplotlib.style.available`.


import matplotlib as mpl
mpl.style.available

Out[1]: ['seaborn-talk',
'seaborn-poster',
'_classic_test',
'seaborn-ticks',
'seaborn-paper',
'ggplot',
'seaborn',
'seaborn-dark',
'seaborn-bright',
'seaborn-pastel',
'fivethirtyeight',
'Solarize_Light2',
'classic',
'grayscale',
'bmh',
'seaborn-dark-palette',
'seaborn-whitegrid',
'seaborn-white',
'dark_background',
'seaborn-muted',
'fast',
'seaborn-notebook',
'seaborn-darkgrid',
'seaborn-colorblind',
'seaborn-deep']

Applying a style sheet

We can call plt.style.use(stylename) to apply a style. This function takes in built-in style sheets, local paths, and URLs.

Creating own style sheet

You can also create your own style sheet...

Aesthetics and readability considerations in styling


As visualization is about delivering messages, the more we think from the reader's perspective, the more effective it will be. An attractive graphic catches more attention. The easier to read a plot is, the more likely are readers to understand the message. Here are several basic principles in designing data plots.

Suitable font styles

The hierarchy can use no more than three levels of font family, weight, and sizes. Use less fancy font families, Sans Serif font if possible. Make sure the font size is large enough to be legible

Note

Serif versus Sans Serif Serif means decorative edges on alphabets. And sans means without in French. As the names imply, Sans Serif fonts are plainer and more simplistic than Serif fonts in general. Let's take the most popular examples of default fonts in Microsoft Office. Times New Roman used in Office 2007 and before is a Serif font, whereas the newer Calibri is a Sans Serif font.

Effective use of colors

  • Use sharper...

Summary


Congratulations! You have now mastered the most commonly used plots and the basic methods to customize plots. We are now ready to move on to more advanced Matplotlib usage.

In the next chapter, we will cover more plot types with the help of third-party packages, methods to optimize displays for multiple plots and axes in certain scales, as well as showing pixels in images. Stay tuned!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Matplotlib for Python Developers. - Second Edition
Published in: Apr 2018Publisher: PacktISBN-13: 9781788625173
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim

author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu