Instant Access To Hands-On Matplotlib: Learn Plotting and Visualizations With Python 3 Ashwin Pajankar Ebook Full Chapters
Instant Access To Hands-On Matplotlib: Learn Plotting and Visualizations With Python 3 Ashwin Pajankar Ebook Full Chapters
com
https://ebookmeta.com/product/hands-on-matplotlib-learn-
plotting-and-visualizations-with-python-3-ashwin-pajankar/
OR CLICK BUTTON
DOWLOAD NOW
https://ebookmeta.com/product/hands-on-machine-learning-with-
python-implement-neural-network-solutions-with-scikit-learn-and-
pytorch-1st-edition-ashwin-pajankar/
https://ebookmeta.com/product/raspberry-pi-image-processing-
programming-with-numpy-scipy-matplotlib-and-opencv-2nd-edition-
ashwin-pajankar-2/
https://ebookmeta.com/product/raspberry-pi-image-processing-
programming-with-numpy-scipy-matplotlib-and-opencv-2nd-edition-
ashwin-pajankar/
https://ebookmeta.com/product/python-unit-test-automation-
automate-organize-and-execute-unit-tests-in-python-ashwin-
pajankar/
Python Unit Test Automation: Practical Techniques for
Python Developers and Testers 1 / converted Edition
Ashwin Pajankar
https://ebookmeta.com/product/python-unit-test-automation-
practical-techniques-for-python-developers-and-
testers-1-converted-edition-ashwin-pajankar/
https://ebookmeta.com/product/raspberry-pi-image-processing-
programming-develop-real-life-examples-with-python-pillow-and-
scipy-1st-edition-ashwin-pajankar/
https://ebookmeta.com/product/learn-quantum-computing-with-
python-and-q-a-hands-on-approach-1st-edition-sarah-c-kaiser/
https://ebookmeta.com/product/practical-linux-with-raspberry-pi-
os-quick-start-1st-edition-ashwin-pajankar/
https://ebookmeta.com/product/python-data-analytics-with-pandas-
numpy-and-matplotlib-3rd-edition-fabio-nelli/
Hands-on
Matplotlib
Learn Plotting and Visualizations
with Python 3
—
Ashwin Pajankar
Hands-on Matplotlib
Learn Plotting and Visualizations
with Python 3
Ashwin Pajankar
Hands-on Matplotlib
Ashwin Pajankar
Nashik, Maharashtra, India
Introduction�����������������������������������������������������������������������������������������������������������xvii
v
Table of Contents
vi
Table of Contents
Bar Graphs���������������������������������������������������������������������������������������������������������������������������������� 92
Scatter Plot��������������������������������������������������������������������������������������������������������������������������������� 95
Summary������������������������������������������������������������������������������������������������������������������������������������ 97
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Chapter 18: Visualizing Real-Life Data with Matplotlib and Seaborn������������������� 269
COVID-19 Pandemic Data���������������������������������������������������������������������������������������������������������� 269
Fetching the Pandemic Data Programmatically������������������������������������������������������������������������ 271
Preparing the Data for Visualization������������������������������������������������������������������������������������������ 275
Creating Visualizations with Matplotlib and Seaborn���������������������������������������������������������������� 276
Creating Visualizations of Animal Disease Data������������������������������������������������������������������������ 287
Summary���������������������������������������������������������������������������������������������������������������������������������� 292
Index��������������������������������������������������������������������������������������������������������������������� 293
x
About the Author
Ashwin Pajankar earned a Master of Technology in computer science engineering from
IIIT Hyderabad and has more than 25 years of experience in the area of programming.
He started his journey in programming and electronics at the tender age of 7 with the
BASIC programming language and is now proficient in Assembly programming, C, C++,
Java, shell scripting, and Python. His other technical expertise includes single-board
computers such as the Raspberry Pi and Banana Pro, microcontroller boards such as the
Arduino, and embedded boards such as the BBC Micro Bit.
He is currently a freelance online instructor teaching programming to more
than 70,000 professionals. He also regularly conducts live programming bootcamps
for software professionals. His growing YouTube channel has an audience of more
than 10,000 subscribers. He has published more than 15 books on programming and
electronics.
In addition to his technology work, he volunteers for many social causes. He has
won several awards at his university and past workplaces for his community service.
He has also participated in many industry–institute linkage programs, connecting his
past employers with his alma maters. During the COVID-19 pandemic (which was
unfolding at the time of writing of this book), he participated in and led many initiatives
to distribute essential supplies and medicine to needy people in his local community.
xi
About the Technical Reviewer
Joos Korstanje is a data scientist with more five years of industry experience in
developing machine learning tools, especially forecasting models. He currently works
at Disneyland Paris where he develops machine learning for a variety of tools. He is the
author of the book Advanced Forecasting with Python.
xiii
Acknowledgments
I would like to thank Celestin and Aditee for giving me an opportunity to share my
knowledge and experience with readers. I thank James Markham for helping me to
shape this book according to Apress standards. I am in debt to the technical reviewer for
helping me to improve this book. I also thank Prof. Govindrajulu Sir’s family, Srinivas
(son) and Amy (daughter in law), for allowing me to dedicate this book to his memory
and for sharing his biographical information and photograph for publication. I would
also like to thank all the people at Apress who were instrumental in bringing this project
to reality.
xv
Introduction
I have been working in the domain of data science for more than a decade now, and I
was introduced to Python more than 15 years ago. When I first worked with libraries
such as NumPy, Matplotlib, and Pandas, I found it a bit tedious to comb through all
the available literature in the form of printed books, video tutorials, and online articles,
as most of them lacked comprehensive steps for beginners. It was then that I resolved
to write a book, and I am glad that I could bring my resolution to life with the help of
Apress.
This book is the result of thousands of hours (in addition to the ones spent writing
the actual book) going through technical documentation, watching training videos,
writing code with the help of different tools, debugging faulty code snippets, posting
questions and participating in discussions on various technical forums, and referring
to various code repositories for pointers. I have written the book in such a way that
beginners will find it easy to understand the topics. The book has hundreds of code
examples and images of code output so that you can fully understand each concept
introduced. All the code examples are explained in detail.
The book begins with a general discussion of Python and a small guide explaining
how to install it on various computing platforms such as the Windows OS and Linux
computers (like the Raspberry Pi). We then move on to discussing the scientific
ecosystem. Then we focus on NumPy, which is the fundamental library for numerical
computing. We specifically focus on the multidimensional, array-like data structure
of NumPy, called the Ndarray. We then explore data visualization libraries, such as
Matplotlib and Plotly, to learn how to plot Ndarrays.
Most of the chapters explore the data visualization library Matplotlib. You will learn a
lot of data visualization tips and techniques in these chapters.
Then we dive into Pandas so you can learn about its important data structures, called
the series and dataframe. Midway through the book, you will also learn how to read data
from various data sources using Python, NumPy, Matplotlib, and Pandas. You will also
learn how to visualize Pandas data with popular visualization libraries such as Matplotlib
and Seaborn, as well as how to work with time-indexed data.
xvii
Introduction
xviii
CHAPTER 1
Introduction to Python 3
I welcome you all to the exciting journey of data visualization with Matplotlib and related
libraries such as NumPy, Pandas, and Seaborn.
This chapter covers the basics of the Python programming language including its
history, installation, and applications. You will be writing a few simple and introductory
Python 3 programs and be learning how to execute them on various OS platforms.
Then, we will start exploring the scientific Python ecosystem. We will briefly discuss
the member libraries of the scientific Python ecosystem, and toward the end, we will
explore Jupyter Notebook so we can use it throughout the rest of the book.
Specifically, the following are the topics covered in this chapter:
• Python modes
• Python IDEs
• Anaconda
After reading this chapter, you will be comfortable with the installation and the basic
usage of the Python 3 programming language in various modes on various platforms.
1
© Ashwin Pajankar 2022
A. Pajankar, Hands-on Matplotlib, https://doi.org/10.1007/978-1-4842-7410-1_1
Chapter 1 Introduction to Python 3
As you can see, Python 2.x versions are no longer supported, as Python 2 is retired.
Python 3 is not backward compatible with Python 2. Python 3 is the latest and supported
version of the Python programming language. So, we will use Python 3 programming
throughout the book to demonstrate the concepts covered. Unless explicitly mentioned,
Python means Python 3 throughout this book.
2
Chapter 1 Introduction to Python 3
https://www.python.org/dev/peps/
https://www.python.org/dev/peps/pep-0001/
• Readability counts.
• Although that way may not be obvious at first unless you’re Dutch.
3
Chapter 1 Introduction to Python 3
These are the general philosophical guidelines that continue to influence the
development of the Python programming language.
A
pplications of Python
As you have learned, Python is a general-purpose programming language; it has
numerous applications in the following areas:
• Web development
• GUI development
• Software development
• System administration
4
Chapter 1 Introduction to Python 3
I think now is a good time to discuss various Python distributions. You saw that
the actual interpreter program for Python is known as an implementation. When it
is bundled with a few useful things such as an integrated development environment
(IDE), tools, and libraries, it is known as a distribution. You can find the list of Python
distributions at https://wiki.python.org/moin/PythonDistributions.
Now, let’s look at how to install Python on both platforms.
Run the setup file to install Python 3. During installation, select the check box related
to adding Python 3 to the PATH variable (Figure 1-2).
5
Chapter 1 Introduction to Python 3
Also, choose the “Customize installation” option. That will take you to more options,
as shown in Figure 1-3.
6
Chapter 1 Introduction to Python 3
Select all the boxes and click the Next button to continue the setup. Complete the
setup. The name of the binary executable program for Python is python on Windows.
Once the installation completes, run the following command at the Windows command
prompt, cmd:
python -V
Python 3.8.1
pip3 -V
A recursive acronym, pip stands for “Pip installs Python” or “Pip installs packages.”
It is a package manager for the Python programming language. You can install the other
needed Python libraries for our demonstrations using the pip utility.
7
Chapter 1 Introduction to Python 3
To find out the exact location of Python, you can run the where command as follows:
where python
C:\Users\Ashwin\AppData\Local\Programs\Python\Python38-32\python.exe
Similarly, you can find out the location of the pip3 utility by running the following
command:
where pip3
We will be using this utility heavily throughout the book to install and manage
Python 3 libraries on the computer we are working with. The following command lists all
the installed packages:
pip3 list
python3 -V
pip3 -V
which python3
which pip3
Almost all the other popular Linux distributions come with Python pre-installed too.
8
Chapter 1 Introduction to Python 3
The other way is to launch it from the command prompt (cmd) by running the
following command:
idle
Before proceeding, you need to customize IDLE so that it works for you. You can
change the font by selecting Options ➤ Configure IDLE, as shown in Figure 1-6.
The window shown in Figure 1-7 opens so you can change the font and size of the
characters in IDLE.
10
Chapter 1 Introduction to Python 3
11
Chapter 1 Introduction to Python 3
Once the installation is complete, you can find IDLE in the menu (in this case the
Raspberry Pi OS menu), as shown in Figure 1-8.
You can also launch IDLE on Linux by running the following command:
idle
12
Chapter 1 Introduction to Python 3
I nteractive Mode
Python’s interactive mode is like a command-line shell that executes the current
statement and gives immediate feedback on the console. It runs the statements given
to it immediately. As new statements are fed into and executed by the interpreter, the
code is evaluated. When you open IDLE, you will see a command-line prompt. This is
Python’s interactive mode. To see a simple example, let’s type in the customary Hello
World program in the interactive prompt as follows:
print('Hello World!')
Press the Enter key to feed the line to the interpreter and execute it. Figure 1-9 shows
the output.
You can launch Python’s interactive mode from the command prompt too. At
the Linux command prompt (e.g., lxterminal), run the command python3, and at the
Windows command prompt (cmd), run the command python. Figure 1-10 shows the
interactive mode at the Windows command prompt.
13
Chapter 1 Introduction to Python 3
Script Mode
You can write a Python program and save it to disk. Then you can launch it in multiple
ways. This is known as script mode. Let’s demonstrate it in IDLE. You can use any text
editor to write the Python program. But as IDLE is an IDE, it is convenient to write and
run the Python programs using IDLE. Let’s see that first. In IDLE, select File ➤ New File.
This will create a new blank file. Add the following code to it:
print('Hello World!')
Then save it with the name prog01.py on the disk (Figure 1-11).
In the menu, select Run ➤ Run Module. This will execute the program at IDLE’s
prompt, as shown in Figure 1-12.
14
Chapter 1 Introduction to Python 3
You can even launch the program with Python’s interpreter at the command prompt
of the OS. Open the command prompt of the OS and navigate to the directory where the
program is stored. At the Windows command prompt, run the following command:
python prog01.py
In the Linux terminal, you must run the following command prompt:
python3 prog01.py
Then the interpreter will run the program at the command prompt, and the output
(if any) will appear there.
In Linux, there is another way you can run the program without explicitly using the
interpreter. You can add a shebang line to the beginning of the code file. For example, say
our code file looks like this:
#!/usr/bin/python3
print('Hello World!')
The first line is known as a shebang line. It tells the shell what interpreter to use and
its location. Then run the following command to change the file permission to make it
executable for the owner as follows:
Then you can directly launch your Python program file like any other executable
with ./, as follows:
./prog01.py
15
Chapter 1 Introduction to Python 3
The shell will execute the program and print the output in the terminal. Note
that this is applicable only for Unix-like systems (Linux and macOS) as they support
executing programs like this. You will learn more about Python programming
throughout the book.
• Mu Editor (https://codewith.mu/)
All these IDEs and plugins are free to download and use. As an exercise for this
chapter, you may want to explore them to find the IDE you are most comfortable with.
16
Chapter 1 Introduction to Python 3
17
Chapter 1 Introduction to Python 3
known as Jupyter Notebook. It is a server program that can create interactive notebooks
in a web browser.
Jupyter Notebook is a web-based notebook that is used for interactive programming
of various programming languages like Python, Octave, Julia, and R. It is popular
with people who are working in research domains. Jupyter Notebook can save code,
visualizations, output, and rich text in a single file. The advantage of Jupyter Notebook
over Python’s own interactive prompt is that you can edit the code and see the new
output instantly, which is not possible in Python’s interactive mode. Another advantage
is that you have the code, rich-text elements, and output of the code (which can be in
graphical or rich-text format) in the same file on disk. This makes it easy to distribute.
You can save and share these notebooks over the Internet or using the portable storage
equipment. There are many services online that help to store and execute your notebook
scripts on cloud servers.
Let’s see how you can use Jupyter Notebook for writing and executing Python
statements now. Run the following command in the command prompt of the OS to
launch the Jupyter Notebook server process there:
jupyter notebook
The Jupyter Notebook server process will be launched, and the command prompt
window shows a server log, as in Figure 1-13.
18
Chapter 1 Introduction to Python 3
Also, it launches a web page in the default browser of the OS. If the browser window
is already open, then it launches the page in a new tab of the same browser window.
Another way to open the page (in case you close this browser window running Jupyter
Notebook) is to visit http://localhost:8888/ in your browser. It displays the page
shown in Figure 1-14.
19
Chapter 1 Introduction to Python 3
file:///C:/Users/Ashwin/AppData/Roaming/jupyter/runtime/nbserver-8420-open.html
http://localhost:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c
http://127.0.0.1:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c
In the previous log, you can see a couple of URLs. They refer to the same
page (localhost and 127.0.0.1 are the same hosts). Either you can directly copy
and paste any of these URLs directly in the address bar of the browser tab and
open the Jupyter Notebook home page or you can visit http://localhost:8888/
as discussed earlier and then paste the token in the server log (in our case it is
e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c) and log in. This will take you
to the same home page.
20
Chapter 1 Introduction to Python 3
Note that every instance of the Jupyter Notebook server will have its own token, so
the token shown in the book will not work with your notebook. The token is valid only
for that server process.
So, if you follow any one of the routes explained earlier, you will see a home page tab
in the browser window, as shown in Figure 1-15.
As you can see, there are three tabs on the web page: Files, Running, and Clusters.
The Files tab shows the directories and files in the directory from where you launched
the notebook server from the command prompt. In the previous example, I executed
the command jupyter notebook from lxterminal of my Raspberry Pi. And the current
working directory is the home directory of the pi user /home/pi. That is why you can
see all the files and directories in the home directory of my Raspberry Pi computer in
Figure 1-15.
In the top-right corner, you can see the Quit and Logout buttons. If you click the
Logout button, then it logs out from the current session, and to log in, you again need
the token or URL with the embedded token from the notebook server log, as discussed
earlier. If you click the Quit button, then it stops the notebook server process running at
the command prompt and displays the modal message box shown in Figure 1-16.
21
Chapter 1 Introduction to Python 3
Figure 1-16. The message shown after clicking the Quit button
To work with the Jupyter Notebook, you need to execute the command jupyter
notebook again at the command prompt.
On the top-right side, just below the Quit and Logout buttons, you can see a small
button with a refresh symbol. This button refreshes the home page. You also have the
New button. Once clicked, it shows a drop-down, as shown in Figure 1-17.
As you can see, the drop-down is divided into two sections, Notebook and Other. You
can create the Octave and Python 3 notebooks. If your computer has more programming
languages installed that are supported by Jupyter Notebook, then all those languages
will show up here. You can also create text files and folders. You can open a command
prompt in the web browser by clicking Terminal. Figure 1-18 shows lxterminal running
in a separate web browser tab.
22
Chapter 1 Introduction to Python 3
23
Chapter 1 Introduction to Python 3
If you go to the home page again by clicking the home page tab in the browser and
then open the Running tab in the home page, you can see the entries corresponding to
the terminal and the Python 3 notebook, as shown in Figure 1-20.
printf("Hello, World!\n");
Then click the Run button. Jupyter will execute the statement as a Python 3
statement and show the result immediately below the cell, as shown in Figure 1-21.
As you can see, after execution, it automatically creates a new cell below the
result and sets the cursor there. Let’s discuss the menu bar and the icons above the
programming cells. You can save the file by clicking the floppy disk icon. You can add a
new empty cell after the current cell by clicking the + icon. The next three icons are Cut,
Copy, and Paste. Using the up and down arrows can shift the position of the current cell
up and down, respectively. The next option is to run the cell, which you already saw. The
next three icons are for interrupting the kernel, restarting the kernel, and rerunning all
the cells in the notebook. Next to that, you have a drop-down that tells you what type of
cell it should be. Figure 1-22 shows the drop-down when clicked.
The cell is treated as a Python 3 code cell when you choose the Code option. It is
treated as a Markdown cell when you choose the Markdown option. Markdown is a
markup language that can create rich-text output. For example, anything followed by #
creates a heading, anything followed by ## creates a subheading, and so on. Just type the
following lines in a Markdown cell and execute them:
# Heading 1
## Heading 2
During our Python 3 demonstrations, we will mostly use Markdown for headings.
However, you can further explore Markdown on your own by visiting https://jupyter-
notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20
Markdown%20Cells.html. Figure 1-23 shows the output of the previous demonstration.
25
Chapter 1 Introduction to Python 3
You can even change the name of the notebook file by clicking its name in the top
part of the notebook. Once you click, you’ll see a modal box for renaming, as shown in
Figure 1-24.
Rename the notebook if you want. If you browse the location on disk from where you
launched the Jupyter Notebook from at the command prompt, you will find the file with
the .ipynb extension (meaning “IPython notebook”).
In the same way, you can use Jupyter Notebook for doing interactive programming
with the other programming languages that support Jupyter. We will mostly use this
notebook format to store our code snippets for interactive sessions. This is because
everything is saved in a single file that can be shared easily, as discussed earlier.
26
Chapter 1 Introduction to Python 3
You can clear the output of a cell or the entire notebook. In the menu bar, click the
Cell menu. In the drop-down, Current Outputs and All Output have a Clear option that
clears the output of cells. Figure 1-25 shows the options.
One of the most significant advantages of Jupyter Notebook is that you can edit an
already executed cell if there is any syntax error or you simply want to change the code.
Jupyter Notebook is like an IDE that runs within a web browser and produces the output
in the same window. This interactivity and facility to keep code, rich text, and output in
the same file has made Jupyter Notebook project hugely popular worldwide. The kernel
for running Python programs comes from the IPython project. As I mentioned earlier,
you can use it for other programming languages too. I have used it for running GNU
Octave programs.
You can find more information about Jupyter Notebook and IPython at the
following URLs:
https://jupyter.org/
https://ipython.org/
27
Chapter 1 Introduction to Python 3
A
naconda
Before we can conclude the chapter, we need to discuss the Python distributions. A
Python distribution is nothing more than the Python interpreter bundled with Python
libraries. One such popular distribution is Anaconda. You can download and install
Anaconda on Linux, Windows, and macOS. Anaconda has many versions. One of them
is free and meant for individual usage. You can find it at https://www.anaconda.com/
products/individual.
Anaconda comes with an open source package manager that can install packages for
Python and other programs. It is known as Conda. You can find more information about
the Conda package manager at https://docs.conda.io/en/latest/.
If you have already installed Python from Python’s website, I recommend using
another computer to install Anaconda. Having multiple interpreters and distributions of
Python can be confusing.
S
ummary
In this chapter, you learned the basics of Python programming language. You learned
how to write basic Python programs and how to execute them in various ways. You
learned to work with Python on various operating systems such as Windows and Linux.
You also learned various modes of the Python programming language and how to launch
Python from the command prompts of various operating systems. You learned the basics
of the built-in package manager of Python, called pip. We also briefly discussed other
IDEs for Python.
Then, you got a brief introduction to the scientific Python ecosystem. We will explore
many components of this ecosystem in the coming chapters. You also learned how to
install Jupyter Notebook on various platforms and explored how you can run simple
Python statements in Jupyter Notebook. You learned that you can store the code and the
output of the same code in a single file that can be shared easily over the Internet and
other media such as portable storage devices.
In the next chapter, we will get started with NumPy.
28
CHAPTER 2
• Ndarray properties
• NumPy constants
Throughout the remaining chapters of this book, we will explore many components
of the scientific Python ecosystem one by one. Throughout this book, we will be
using different libraries that are part of this scientific Python ecosystem. The valuable
knowledge you will gain in this chapter serves as a foundation for the rest of the chapters.
As this is an introductory chapter for a broad ecosystem, I have kept it short yet practical.
N
umPy and Ndarrays
NumPy is the fundamental package for numerical computation in Python. We can
use it for numerical computations. The most useful feature of the NumPy library is the
multidimensional container data structure known as an Ndarray.
An Ndarray is a multidimensional array (also known as a container) of items that
have the same datatype and size. We can define the size and datatype of the items at the
time of creating the Ndarray. Just like other data structures such as lists, we can access
the contents of an Ndarray with an index. The index in an Ndarray starts at 0 (just like
arrays in C or lists in Python). We can use Ndarrays for a variety of computations. All the
29
© Ashwin Pajankar 2022
A. Pajankar, Hands-on Matplotlib, https://doi.org/10.1007/978-1-4842-7410-1_2
Chapter 2 Getting Started with NumPy
other libraries in the scientific Python ecosystem recognize and utilize NumPy Ndarrays
and associated routines to represent their own data structures and operations on them.
Let’s get started with the hands-on material. Create a new notebook for this chapter.
Run the following command to install the NumPy library on your computer:
import numpy as np
You can create a list and use it to create a simple Ndarray as follows:
l1 = [1, 2, 3]
x = np.array(l1, dtype=np.int16)
Here you are creating an Ndarray from a list. The datatype of the members is a 16-bit
integer. You can find the detailed list of datatypes supported at https://numpy.org/
devdocs/user/basics.types.html.
You can write the previous code in a single line as follows:
Let’s print the value of the Ndarray and its type (which, we know, is an Ndarray).
print(x)
print(type(x))
[1 2 3]
<class 'numpy.ndarray'>
30
Other documents randomly have
different content
Mr. Clegg: Supposing that at the trial this man is not defended—a
state of things which is not at all unlikely.
The Stipendiary: You know very well that in that case the judge
would order some learned counsel to defend the prisoner. I am not
going to deal with a state of circumstances that may arise at the
trial. It would be presumption in me to provide for want of justice
before the judges.
Mr. Clegg: This is a preliminary inquiry, and I can put in whatever
I think is for the benefit of the prisoner. Though it is only a
preliminary inquiry I am bound to do it.
The Stipendiary: I rule that sufficient has been asked about those
lettters.
Mr. Clegg: I shall persist in asking these questions until I am
stopped.
The Stipendiary: Then I stop them now. You have quite enough
for your purpose.
Mr. Clegg: I don’t think I have.
The Stipendiary: You have quite enough. You can prosecute her
for perjury if she has spoken falsely as to what has been asked of
her. There are two very particular points, on which she has decisively
spoken in reference to these letters.
Prisoner: She has done more than that.
The Stipendiary: You may if you have cause, indict her for
perjury; it can lead to nothing else.
Mr. Clegg: Then at present I don’t think I have got sufficient, in
my opinion, to test this witness’s credibility.
The Stipendiary: In my opinion, you have. If what you have
asserted is shown to be true, you have more than sufficient to
damage her credibility, and more than sufficient to have a cause for
indicting her for perjury. Beyond that it is not necessary for you to
go, and I rule you shall not go.
Mr. Clegg: How can I indict the woman for perjury unless I put
the letters in her hands?
The Stipendiary: She has looked at them, because I told her to
look at them myself. I said to her, “Look at each one, and see if it is
not in your handwriting.” That is in itself sufficient.
Mr. Clegg: Those letters have been already produced by the
prosecution. I have the right to call for those to be read, and if you
will not now let me cross-examine her in reference to them in detail,
then I ask that the letters be read; then I can cross-examine upon
them, and that comes to the same thing.
The Stipendiary: You should have done that before. It is too late
now. I cannot have them read now.
Mr. Clegg: Put that decision on the depositions. I ask that those
letters put in by the prosecution be read by the clerk of the court.
The Stipendiary: You have seen them.
Mr. Clegg: I have not seen them. I have not had the opportunity
of reading these original letters. By mere favour I have had copies of
them sent to me.
Prisoner: It is nothing but injustice.
The Stipendiary: You will take the ruling of the Court, Mr. Clegg, if
you please, and have done with it.
Mr. Clegg: I put it to you as a matter of law.
The Stipendiary: I have given my decision.
Mr. Clegg: I object to proceed until these letters are read.
Prisoner: Hear, hear.
The Stipendiary: You can proceed with your cross-examination.
Mr. Clegg: I have a right to have the letters read if I please.
The Stipendiary: You can read them over yourself if you like.
Mr. Clegg: If they are put into my hands I shall read them to
witness.
The Stipendiary: Then you may read them.
Mr. Clegg: Very well, then; that is all I want. The witness has
denied that she has had anything to do with them. (To witness):
Have you read them?—Some of them.
Have you read them all?—Not all of them.
Then I will read them to you.
Mr. Pollard: There will be no necessity for their being read aloud.
Let the witness read them for herself.
Mr. Clegg: I will read this one to you now, if you please.
(Reading): “If you have a note for me send now whilst he is out, but
you must not venture, for he is watching, and you cannot be too
careful. Hope your foot is better. I went to Sheffield yesterday, but I
could not see you anywhere. Were you out? Love to Jane.” Did you
write that letter?—No.
Mr. Clegg: Now I put that letter in. Have you had an envelope in
your possession like that (handing an envelope to the witness)?—I
don’t know; perhaps I might have. As regards the envelope, the
prisoner used to come for paper and writing materials to my house.
Prisoner: No, I did not; oh, no.
Mr. Clegg (holding a yellow envelope in his hand): Have you read
the contents of this?—Yes, excepting something I could not make
out.
Will you swear that is not your handwriting?—I swear there are
none of them in my writing.
Mr. Pollard: I think you need hardly put it to her now, because she
has sworn over and over again that there are none of them in her
writing.
Mr. Clegg (to witness): Did you ever give the prisoner an
American cent?—No.
Did you ever borrow any money from him?—No.
Not at all?—I never did.
Mr. Clegg, reading: “Things are looking very bad, for people told
him everything. (Then there is something missing.) Pick out F. D.” Do
you know what that means?—No.
Mr. Clegg (again reading): “Do keep quiet, and don’t let any one
see you.” You did not write that? No; I did not write that.
The Stipendiary: She says she didn’t write these letters.
Mr. Clegg: Well, I wish to question her on the point.
The Stipendiary: I will not have it, and I will have my ruling
attended to. It shall not go down on the depositions.
Cross-examination continued: You had a son named Willie?—
William Henry.
How old is he?—Seven years old last December.
Did the prisoner ever give your son any halfpennies or coppers
that you know of?—No, not that I am aware of.
You have seen that letter as well (handing a letter to witness)?—
Yes, I have seen them all.
Do you know a woman named Norton?—Norton! I seem to
remember the name. I think I remember the name.
Did you ever write this to the prisoner, “Mrs. Norton is raising h
—— about what I——(then follows a blank)——. Can you settle it,
and send me the prints?”
Mr. Pollard: Witness denied that she ever wrote the letter, and the
magistrate has given a ruling.
Mr. Clegg (to witness): When you were before the magistrates
last week you said that you left Darnall in consequence of the
prisoner annoying you?—Yes, on account of his annoyance.
Had you seen him from July, 1876 up to the time when you left in
October?—No.
Did you know where he was?—No, I did not. He made himself
scarce on account of the warrant I had taken out against him.
Did his family leave the neighbourhood before you left?—I don’t
know.
What do you now say was the reason for your leaving Darnall?
Because we were afraid of him. That was the reason. We thought he
might come in in the night. We thought we had better go where he
would not know where to find us.
It was because you were afraid of him?—Yes; he had threatened
both my life and that of my husband.
Was the sole reason you left because you were afraid of him?—
Yes; and we were told that he visited Darnall in female attire.
Did you say that the prisoner had threatened to blow your brains
out, and those of your husband?—Yes; I said that.
Will you say that he threatened to blow your husband’s brains
out?—Yes; I will swear that threatened to blow out both my brains
and my husband’s.
Did you state that before the coroner?—The case was not brought
before the coroner.
Were you not examined before the coroner?—Yes; on the case of
murder, nothing more.
Were you examined as to the death of your husband?—Yes.
Did you say then that the prisoner had threatened your life at all?
—Not that I remember.
The depositions of the witness were then put in by Mr. Pollard,
from which it appeared that before the coroner witness had said that
Peace had threatened to blow out both her brains and her
husband’s.
Had there been any quarrel between prisoner and your husband?
—I say he had been very annoying.
Was there any quarrel?—I can’t say there was any quarrel,
because my husband would not speak to him.
Have you ever received a letter from the prisoner at all?—No sir.
Oh! yes. I have received threatening letters.
Have you got them?—Mr. Chambers has them, I think.
I am now talking about the night of the murder. Previous to your
going into the closet had your son been taken to bed, do you know?
—Yes.
Did you see the prisoner when you were in the bedroom?—No.
When you were coming out of the closet did you see the
prisoner?—Yes, I did.
Did you say to him, “You old devil, what are you doing here
now?”—I don’t remember saying anything to him, but he said,
“Speak or I’ll fire.”
Will you swear that you did not say, “You old devil, what are you
doing here to-night? I should have thought that you had brought
enough disgrace upon me?—I don’ remember speaking at all. I was
too astonished.
Did he say to you, “I will let you have the notes back again if you
will get him to stay proceedings?”—No.
Did you say, “You know very well he won’t do it, as he has placed
it in the hands of the lawyers?”—No; I did not speak to him.
Your husband then came up?—Yes.
How far was he from you and the prisoner before you first saw
him?—About two or three feet.
How far is the passage from where you were to the closet? Close
by.
Well, how many yards?—I can’t say.
How far were you from the closet door when you saw your
husband?—Only three or four feet; I was just at the end.
Whereabouts was the prisoner when he came up?—The prisoner
was going down the passage.
Was he walking or running away?—He was going at a rather quick
pace.
Was he walking pretty quickly away from your husband?—He was
going down the passage.
Away from your husband?—Away from him.
When your husband was going towards the prisoner did you hear
him say, “If you don’t stop I’ll fire?”—No.
Did you see the prisoner on the ground?—How do you mean?
With your husband. Was there any struggling between your
husband and the prisoner?—No, no.
How far were you away from them when your husband was shot,
did you say?—A few feet off.
Did you hear the prisoner speak to your husband before he fired?
—No, I did not. There was no speaking at all.
Not from the time that he came out right up to the time he was
shot?—No.
Will you swear that your husband and the prisoner were not
struggling together on the ground?—No, they were not struggling.
They were not close enough together.
Mr. Clegg: What became of the lantern that you had?—What
became of my lantern?
Yes, had you it in your hand?—I threw it down.
Before you saw your husband come out had you the lantern still
in your hand?—Yes.
This concluded the cross-examination of Mrs. Dyson.
Mr. Pollard (re-examining): Are there some steps coming from the
passage into the roadway?—There are one or two steps.
When your husband fell in what direction did his head fall?—From
the side of the passage.
Did he take any step forward after the bullet struck him?—No, he
dropped instantly.
At the time the bullet was fired did you see whether Peace was
down on the pathway of the causeway?—Yes.
Therefore he would be some few feet below your husband?—Yes.
He was down off the steps on the causeway when he fired the
second shot.
And you say he would therefore be some two or three feet below
your husband’s head at the time he fired?—Yes.
You were saying something about threatening letters. Between
July and October, 1876, did you see any threatening letters?—Yes.
How many?—I know of two, and there were perhaps more.
Have you those letters?—Mr. Chambers, solicitor, has them. Mr.
Dyson gave them to Mr. Chambers.
You gave them to Mr. Dyson, your husband, and he gave them to
Mr. Chambere?—Yes.
You don’t know whose writing they were?—They were signed as if
from prisoner, and seemed to come from Germany.
(Prisoner: I wish to call witnesses.)
The Stipendiary: It’s not time to call witnesses.
Prisoner: Am I going to be committed to-day? I want my
witnesses called before.
The Stipendiary: It’s not the time.
This concluded Mrs. Dyson’s evidence, who then retired.
Police-constable 235, John Pearson, was next called and
examined by Mr. Pollard. Do you know the prisoner?—I do, three or
four years.
Prisoner: Let that person come up here, will you? I don’t know
him.
Mr. Pollard (to witness): Three or four years from now?—Yes, I
knew him about two years before the murder.
You remember the date of the murder of Mr. Dyson in November,
1876?—Yes.
Did you receive some instructions from your superior officer to go
in search of the prisoner?—I did. I received instructions the same
morning at half past two o’clock.
That was Nov. 30?—Yes.
Did you go to any place in Hull which you had any reason to
know?—Yes, I went to 37, Collier-street.
Who was living there?—Peace’s wife, I believe. The same person
who had been locked up here in the name of Hannah Peace. She
was keeping a shop.
You failed to find him there?—Yes.
Mr. Pollard: I propose now to take the escape and recapture of
the prisoner on the last occasion.
Stipendiary: You have gone after the time.
Mr. Pollard: I put it on the ground of what it is worth, as an
element for a jury to consider, whether an innocent person after
having been examined, and hearing evidence such as was given on
the last occasion, would have attempted to escape as he did.
The Stipendiary: He was a convict, you know, suffering under
sentence of penal servitude for life.
Mr. Pollard: Well, I will not press it.
Mr. Pollard then said that was his case.
Prisoner here exclaimed: “I want my witnesses called. Why don’t
you call my witnesses?” and then fell down.
The depositions were now read over, Mrs. Dyson’s being taken
first.
Whilst her depositions were being read the prisoner conducted
himself in an excited and somewhat insolent manner. Making a faint
of jumping from his chair, he fell back suddenly, calling to be taken
to his cell. The warders stepped forward, and he then refused to be
removed, and called for his counsel, to whom he began to give
instructions in an excited fashion. Mr. Clegg took little notice, and
soon after Peace put his arms on the table, laid his head between
them, and moaned loudly. He next drew the rug over his head, and
made such interruptions that the rug was drawn back, and he was
asked to be still. He paid little attention to this request, but kept
moaning and muttering till the deposition were read over.
Stipendiary (to Peace): Do you hear what is said?
Prisoner (whining): Oh yes, I hear.
The depositions were afterwards read over of Sarah Ann
Colgrave, Mary Ann Gregory, George Brassington, Thomas Wilson,
Police-constable Ward, Inspector Bradbury, and Police-constable
Pearson.
Prisoner: Cannot you call my witnesses? What is the use of my
having witnesses if they are not called?
The Stipendiary: Listen to me.
Prisoner: I cannot have them called.
The Stipendiary: Oh, yes, you can, Listen to me. You first of all
have to make a statement, and then if there are any witnesses to be
called they can be called. Now listen to me. The charge against you
is that you wilfully and of malice aforethought did kill and murder
one Arthur Dyson, on Oct. 29, 1876. Having heard the evidence, do
you wish to say anything in answer to the charge?
Prisoner: Yes; I wish to say that——
Stipendiary: You are not obliged to say anything in answer to the
charge. What you do say will be taken down, and may be given in
evidence against you.
Mr. Clegg: If you take my advice you will simply say not guilty.
The Prisoner: I say I am not guilty, and that justice has not been
done to me so that I can prove I am not guilty. That is what I want.
I want that. I want justice done me. Why don’t they let me call my
witnesses, because you are asking me shall my witnesses be called?
Why should they not be called here? Why? because I have not the
money to pay the expenses.
The Stipendiary: What is it you complain of?
The Prisoner: I want my witnesses called to prove that I have
really not done this.
The Stipendiary: Are there any witnesses to be called?
Prisoner: Yes, sir.
The Stipendiary: Are they here?
Prisoner: Yes, sir.
The Stipendiary: Then you must ask your solicitor.
Mr. Robinson (reading): You say, “I say I am not guilty, and I want
my witnesses called to prove I am not guilty.”
Prisoner: That is what I said. I have lots of witnessses who can
prove that that base, bad, woman has threatened my life, and has
threatened her husband’s life; but I can’t talk to you, I am so bad. I
feel very bad. But she has threatened to take my life often.
The Stipendiary: Is this what you say?
Prisoner: I say I am not guilty, and I say I have not justice done
me to prove I am not guilty, and that I want my witnesses called.
(Loudly.) I say I can prove that I have not threatened her life. She
has threatened her husband’s, and she has pointed pistols and
things at me.
The Stipendiary: Have you any witnesses to-day? You are not
taking your trial to-day. This is only a preliminary examination.
Prisoner: I cannot have those witnesses without I pay for them.
Mr. Clegg: I say I don’t intend calling any witnesses to-day.
The Stipendiary: Do you care to sign your statement? Would you
like to put your name to the statement?
Prisoner: I will try.
Prisoner (to his warder, in a very rough tone of voice): Let me be.
Then, taking up the pen, he said, I cannot see.
Mr. Clegg: Just sign it there.
Prisoner then rested his head on one arm and signed his name
very deliberately, the capital “C” and “P” being particularly well
flourished.
The Stipendiary: You are committed to take your trial on this
charge at the assizes at Leeds.
The Prisoner (with eagerness): When are they?
The Stipendary: They are next week.
Inspector Bradbury was then bound over to prosecute, and the
witnesses to appear at the trial.
The Prisoner: Will you let me sit before the fire a bit before I go?
I am really very bad.
Mr. Clegg: He complains of being cold.
Prisoner: You can put me in irons if you like, but put me near a
fire.
The Chief Constable: The cells are warm enough. It is only in this
corridor that there is so much air.
Mr. Clegg: You will be warm enough in the cell.
Prisoner: I want to see you.
Mr. Clegg: Yes, I will see you.
Prisoner was then removed to the cell, groaning and whining,
apparently overcome by the result of the day’s proceedings.
Mrs. Dyson laboured under suppressed emotion, but bore the
ordeal well. The utmost precautions were taken to keep down
excitement in the town, but popular feeling ran very high.
We have during the progress of this work had occasion to refer in
more than instance to the latitude allowed to counsel on police
examinations. A striking instance of this was made manifest on the
several examinations of the prisoner Webster for the Richmond
murder, and before then, in a lesser degree, perhaps, a similar
instance occurred in the examination of the witnesses brought
forward to give their testimony upon the Bannercross murder.
Peace was a daring and reckless burglar. An adept at disguising
himself in a style that eluded the scrutiny of the police, he set small
value on the vigilance of professional detectives.
The comparative immunity with which he had escaped the
consequences of his iniquity induced him to wax bold in
transgression.
Even to the last there seemed an idea that he might still be able
to baffle justice. Everything a wicked ingenuity could do was done to
discredit the testimony on which he was convicted; but the utmost
license of counsel was unavailing.
We are not disposed to say anything severe about a barrister
struggling with the difficulties Mr. Lockwood was called to combat. In
such circumstances the old adage of “No case—abuse the plaintiff’s
attorney,” is a sufficient explanation of what might otherwise appear
unseemly.
On the present occasion, however, it was not the attorney, but the
press that got abused. In the recent trial of the directors of the City
of Glasgow Bank, a similar policy was pursued by a prominent and
really able advocate.
But the barrister to whom the defence of Charles Peace was
committed passed beyond the duty of counsel in the denunciation of
newspapers.
It was thus that Mr. Lockwood delivered himself:
“Never in the course of my experience has there been such a cry
raised on the part of those who ought to be most careful of all
others in preserving the liberties of their fellow-men and the
independence of the tribunals of justice. I say that in this respect
these parties have proved false to the great duties entrusted to
them, and have not hesitated to raise a merciless cry for blood for
the sake of the paltry pennies which they have been able to extract
from the public, whom they have tried to gull.”
This charge against the press is false. There had really been no
attempt to stimulate public antipathy against the Bannercross culprit.
In point of fact, a judicious critic might with some show of reason
insinuate that the details of the life of Peace had been placed before
the public in aspects more attractive than just.
The convict was not at all averse to the notoriety which he had
recently achieved. But when his life is carefully scanned from the day
that he first enlisted in the “Devil’s Regiment of the Line” until
sentence of death was passed upon him, it is abundantly evident
that the way of the transgressor was hard.
Charles Peace commenced his criminal career before he was
fourteen. He was not yet fifty, but already there was unambiguous
evidence that premature old age was stealing over him.
Though devoted to crime, Peace never consorted with criminals.
This peculiarity in his career constituted his safety. It is mainly
because the haunts and habits of criminals are known that they are
detected.
The impunity with which, for a very considerable period, Peace
was enabled to commit the Blackheath burglaries arose from the air
of mystery with which he was surrounded.
It must not, however, be assumed that there was anything heroic
in the kind of housebreaking with which he was identified.
Houses in the district that formed the scene of his depredations
afford every facility to the “cracksman.” But the good fortune which
had so often favoured Peace in this region at length deserted him.
A constable whom a revolver could not scare mastered the
burglar.
When he was condemned to penal servitude, it was discovered
that the culprit had been guilty of a more serious crime.
“Information received” enabled the authorities to connect the
Blackheath burglar with the Bannercross murder.
That crime was fast fading from the memory of even those
amongst whom it had been committed, and every hope of arresting
the murderer had been abandoned.
Mr. Dyson’s widow was in America, and Peace, who knew this,
thought himself safe.
But, if “justice steals along with woollen feet, it strikes with iron
hands.”
Mrs. Dyson was brought back from the United States to avenge in
the witness-box the brutality of her tormentor.
Her evidence as to all that transpired on the eventful night that
her husband fell was too minute and circumstantial to be shaken by
even the severest cross-examination.
The line of evidence which counsel was instructed to take only
deepened the infamy of the accused.
Even had it been possible to prove an improper intimacy between
Peace and Mrs. Dyson, that would have done nothing to mitigate the
atrocity of Mr. Dyson’s murder.
A most persistent effort was made by Mr. Lockwood to show that,
previous to the firing of the shot from which Mr. Dyson fell, there
had been a struggle between the murderer and his victim.
On this point, however, the widow’s evidence was decisive. There
was indeed no necessity that Mr. Dyson should close with the culprit.
Moreover, inoffensive people are not usually anxious to fight
ruffians who are armed with “six-shooters.”
When Peace was in a difficulty or in a passion, recourse to
firearms seemed perfectly natural. Society was his legitimate prey,
and in presence of unarmed antagonists the burglar waxed valorous.
Rarely has a more wanton murder been perpetrated than that of Mr.
Dyson.
The only offence of which that unfortunate man appeared guilty
was his abhorrence of the attentions of Peace. To escape them, he
removed from the neighbourhood in which the convict dwelt.
The scamp, however, followed the Dysons with a fiendish
malignity. When threatening to blow out Mrs. Dyson’s brains, Peace
had the effrontery to ask a bystander to “bear him witness that she
had struck him with a life-preserver.” This idea was a pure myth.
The preserver existed only in the foul imagination of the criminal,
and the struggle with Mr. Dyson on the 29th November, 1876, a still
bolder fiction. It is possible that the crime was unpremeditated, and
that annoyance rather than murder was intended. But a man who
makes a revolver the instrument of annoyance cannot guard against
the most dreaded contingencies.
Mr. Campbell Foster was justly enough precluded from making
any reference to Peace’s attempted escape from the railway train.
But it is not difficult to understand the motives under which the
desperate leap was taken.
If Peace had possessed any confidence in his ability to support
the plea set up in his behalf, there would have been no attempt to
escape. But he was awnre of what awaited him.
He knew enough of Mrs. Dyson to fear that her testimony was not
likely to be shaken. Every incidence in her career, so far as it was
known to counsel, was reproduced for the purpose of disconcerting
and discrediting her. Nevertheless, she left the witness-box with her
evidence unshaken.
Thus has Charles Peace been condemned. Hunted down while not
yet fifty, there is in every feature of his forbidding face evidence that
such a career is as unprofitable as it is criminal.
It is difficult to conceive what this malefactor might have been
under other and brighter auspices. But, dedicated to crime from his
youth upward, as years passed away, conscience, which in his case
was never tender, became “seared as with a hot iron.”
When the final stage in his trial was reached, and Peace was
asked if he had anything to say, he whimpered out, “Is there any
use of saying anything now?”
But in his cell the convict collapses, and his courage proves
melodramatic.
CHAPTER CLVIII.
One of the most remarkable and daring exploits of our time, and
one which more than any other of his adventures awakened the
public mind to the desperate and reckless nature of the man was his
attempt to escape while in the charge of his warders in the railway
carriage. This might be considered the culminating point of his
lawless life, and had he been without handcuffs, and possessed of
his favourite weapon—a six-chambered revolver—we would not have
given much for the lives of his two custodians. It was indeed
fortunate for them that the wild beast under their care was safely
manacled, and, indeed, the great body of the people had no idea of
the risks run by the servants of the law in dealing with ruffians of
Peace’s type. We subjoin an interesting account given by an officer,
and printed in Chambers’s Journal of