Matplotlib 3.0 Cookbook - GBV

Matplotlib 3.0 Cookbook

Over 150 recipes to create highly detailed interactive visualizations using Python

Srinivasa Rao Poladi

BIRMINGHAM - MUMBAI

Table of Contents

Preface

Chapter 1: Anatomy of Matplotlib

7

Introduction

7

Architecture of Matplotlib

7

Backend layer

8

Artist layer

8

Scripting layer

8

Elements of a figure

8

Figure

9

Axes

10

Axis

10

Label

10

Legend

10

T itle

10

Ticklabels

11

Spines

11

Grid

11

Working in interactive mode

11

Getting ready

11

How to do it...

12

How it works...

13

There's more...

14

Working in non-interactive mode

15

How to do it...

15

How it works...

16

Reading from external files and plotting

17

Getting ready

17

How to do it...

17

Reading from a .txt file

18

Reading from a .csv file

19

Reading from an .xlsx file

19

Plotting the graph

20

How it works...

20

Changing and resetting default environment variables

21

Getting ready

21

How to do it...

22

How it works...

23

There's more...

23

Chapter 2: Getting Started with Basic Plots

25

Introduction

26

Table of Contents

Line plot

26

Getting ready

26

How to do it. ..

26

How it works...

27

There's more...

28

Bar plot

30

Getting ready

30

How to do it. ..

30

How it works...

31

There's more...

32

Scatter plot

33

Getting ready

33

How to do it. ..

34

How it works...

34

There's more...

35

Bubble plot

37

Getting ready

37

How to do it. ..

38

How it works ...

38

Stacked plot

39

Getting ready

39

How to do it. ..

40

How it works ...

40

Pie plot

42

Getting ready

42

How to do it. ..

42

How it works.. .

43

Table chart

44

Getting ready

44

How to do it. ..

44

How it works...

46

Polar plot

47

Getting ready

47

How to do it. ..

48

How it works ...

49

There's more...

50

Histogram

50

Getting ready

51

How to do it. ..

51

How it works.. .

52

There's more...

53

Box plot

54

Getting ready

55

How to do it. ..

55

How it works ...

56

l ii 1

There's more... Violin plot

Getting ready How to do it... How it works... Reading and displaying images Getting ready How to do it... How it works... Heatmap Getting ready How to do it... How it works... Hinton diagram Getting ready How to do it... How it works... Contour plot Getting ready How to do it. .. How it works.. . There's more... Triangulations Getting ready How to do it. .. How it works.. . There's more... Stream plot Getting ready How to do it. .. How it works.. . There's more... Path Getting ready How to do it. .. How it works.. .

Chapter 3: Plotting Multiple Charts, Subplots, and Figures Introduction Plotting multiple graphs on the same axes Getting ready How to do it... How it works... Plotting subplots on the same figure Getting ready

[iii]

Table ofContents

56

57 57 57 58 59 59 59 59 60 60

61 61 63 63 63 64 65 65 66

67 68 69 69 70 70

71

72 72

73 73 74 75 75 76

77

79 79 79 80 80

81

82 82

Table ofContents

How to do it. ..

83

How it works.. .

84

There's more...

85

Plotting multiple figures in a session

88

Getting ready

88

How to do it...

88

How it works...

90

There's more...

92

Logarithmic scale

92

Getting ready

92

How to do it...

92

How it works...

93

There's more.. .

94

Using units of measurement

96

Getting ready

97

How to do it...

97

How it works...

98

There's more...

100

Chapter 4: Developing Visualizations for Publishing Quality

103

Introduction

103

Color, line style, and marker customization

104

Getting ready

104

How to do it. ..

104

How it works...

106

Working with standard colormaps

109

Getting ready

109

How to do it. ..

109

How it works...

110

There's more...

111

User-defined colors and colormaps

114

Getting ready

114

How to do it...

115

How it works...

116

There's more...

118

Working with legend

120

Getting ready

120

How to do it...

120

How it works...

123

There's more.. .

125

Customizing labels and titles

126

Getting ready

126

How to do it...

126

How it works...

128

There's more...

129

[iv]

Table ofContents

Using autoscale and axis limits

129

Getting ready

129

How to do it...

130

How it works...

131

Customizing ticks and ticklabels

132

Getting ready

132

How to do it...

132

How it works...

133

There's more...

134

Customizing spines

136

Getting ready

136

How to do it...

137

How it works...

138

Twin axes

139

Getting ready

139

How to do it...

140

How it works...

141

There's more...

144

Using hatch

146

Getting ready

146

How to do it...

146

How it works...

146

Using annotation

147

Getting ready

147

How to do it...

147

How it works...

148

Using style sheets

149

Getting ready

149

How to do it. ..

149

How it works...

150

There's more...

151

Chapter 5: Plotting with Object-Oriented API

155

Introduction

155

Plotting a correlation matrix using pyplot and object-oriented APls 156

Getting ready

156

How to do it...

157

How it works...

159

Plotting patches using object-oriented API

161

Getting ready

162

How to do it...

162

How it works...

163

Plotting collections using object-oriented API

164

Getting ready

164

How to do it...

165

[v]

Table ofContents

How it works...

166

Chapter 6: Plotting with Advanced Features

169

Using property cycler

169

Getting ready

170

How to do it...

170

How it works...

171

There's more...

172

Using Path effects

173

Getting ready

173

How to do it...

173

How it works...

174

There's more...

175

Using transforms

176

Transforming data co-ordinates to display co-ordinates

176

Getting ready

177

How to do it. ..

177

How it works...

178

There's more...

180

Using axes and blended co-ordinate system transforms

180

Getting ready

180

How to do it. ..

180

How it works...

182

Taking control of axes positions

184

Getting ready

184

How to do it. ..

184

How it works...

186

GridSpec for figure layout

187

Using GridSpec

187

Getting ready

188

How to do it. ..

188

How it works.. .

190

There's more...

191

GridSpec alignment

193

Getting ready

193

How to do it. ..

193

How it works ...

195

Constrained layout

198

Getting ready

198

How to do it. ..

198

How it works...

200

Using GridSpecFromSubplotSpec

202

Getting ready

202

How to do it. ..

202

How it works...

203

Using origin and extent for image orientation

204

Getting ready

205

[ vi I

Table of Contents

How to do it. ..

205

How it works...

206

Geographical plotting using geopandas

207

Getting ready

208

How to do it. ..

208

How it works...

209

Chapter 7: Embedding Text and Expressions

211

Introduction

211

Using mathematical expressions with a font dictionary

212

Getting ready

212

How to do it. ..

212

How it works...

213

Annotating a point on a polar plot

21 4

Getting ready

214

How to do it...

214

How it works...

215

Using ConnectionPatch

21 7

Getting ready

217

How to do it. ..

217

How it works...

2 18

Using a text box

220

Getting ready

220

How to do it. ..

220

How it works...

221

There's more...

223

Plotting area under an integral curve

225

Getting ready

225

How to do it. ..

226

How it works...

227

Defining custom markers

228

Getting ready

228

How to do it...

229

How it works...

229

Fractions, regular mathematical expressions, and symbols

230

Getting ready

231

How to do it...

231

How it works.. .

233

There's more.. .

235

Word embeddings in two dimensions

237

Getting ready

238

How to do it. ..

238

How it works.. .

239

Chapter 8: Saving the Figure in Different Formats

243

Introduction

243

[vii ]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download