Plotting m u ltiple graphs IN TR OD U C TION TO DATA VISU AL - - PowerPoint PPT Presentation

plotting m u ltiple graphs
SMART_READER_LITE
LIVE PREVIEW

Plotting m u ltiple graphs IN TR OD U C TION TO DATA VISU AL - - PowerPoint PPT Presentation

Plotting m u ltiple graphs IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh Strategies Plo ing man y graphs on common a x es Creating a x es w ithin a g u re Creating s u bplots w ithin


slide-1
SLIDE 1

Plotting multiple graphs

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

Bryan Van de Ven

Core Developer of Bokeh

slide-2
SLIDE 2

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Strategies

Ploing many graphs on common axes Creating axes within a gure Creating subplots within a gure

slide-3
SLIDE 3

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Graphs on common axes

import matplotlib.pyplot as plt plt.plot(t, temperature, 'r') # Appears on same axes plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Temperature & Dew Point') # Renders plot objects to screen plt.show()

slide-4
SLIDE 4

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using axes()

plt.axes([0.05,0.05,0.425,0.9]) plt.plot(t, temperature, 'r') plt.xlabel('Date') plt.title('Temperature') plt.axes([0.525,0.05,0.425,0.9]) plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Dew Point') plt.show()

slide-5
SLIDE 5

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

The axes() command

Syntax: axes( [x_lo, y_lo, width, height] ) Units between 0 and 1 (gure dimensions)

slide-6
SLIDE 6

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using subplot()

slide-7
SLIDE 7

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using subplot()

plt.subplot(2, 1, 1) plt.plot(t, temperature, 'r') plt.xlabel('Date') plt.title('Temperature') plt.subplot(2, 1, 2) plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Dew Point') plt.tight_layout() plt.show()

slide-8
SLIDE 8

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

The subplot() command

Syntax: subplot(nrows, ncols, nsubplot) Subplot ordering: Row-wise from top le Indexed from 1

slide-9
SLIDE 9

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

slide-10
SLIDE 10

Customizing axes

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

Bryan Van de Ven

Core Developer of Bokeh

slide-11
SLIDE 11

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Controlling axis extents

axis([xmin, xmax, ymin, ymax]) sets axis extents

Control over individual axis extents

xlim([xmin, xmax]) ylim([ymin, ymax])

Can use tuples, lists for extents e.g., xlim((-2, 3)) works e.g., xlim([-2, 3]) works also

slide-12
SLIDE 12

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

GDP over time

import matplotlib.pyplot as plt plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.show()

slide-13
SLIDE 13

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using xlim()

plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.xlim((1947, 1957)) plt.show()

slide-14
SLIDE 14

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using xlim() and ylim()

plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.xlim((1947, 1957)) plt.ylim((0, 1000)) plt.show()

slide-15
SLIDE 15

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using axis()

plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.axis((1947, 1957, 0, 600)) plt.show()

slide-16
SLIDE 16

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Other axis() options

Invocation Result

axis('off')

turns o axis lines, labels

axis('equal')

equal scaling on x and y axes

axis('square')

forces square plot

axis('tight')

sets xlim , ylim to show all data

slide-17
SLIDE 17

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using axis('equal')

slide-18
SLIDE 18

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using axis('equal')

plt.subplot(2, 1, 1) plt.plot(x, y, 'red') plt.title('default axis') plt.subplot(2, 1, 2) plt.plot(x, y, 'red') plt.axis('equal') plt.title('axis equal') plt.tight_layout() plt.show()

slide-19
SLIDE 19

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

slide-20
SLIDE 20

Legends, annotations, and styles

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON

Bryan Van de Ven

Core Developer of Bokeh

slide-21
SLIDE 21

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Legends

Legend provide labels for overlaid points and curves

slide-22
SLIDE 22

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using legend()

import matplotlib.pyplot as plt plt.scatter(setosa_len, setosa_wid, marker='o', color='red', label='setosa') plt.scatter(versicolor_len, versicolor_wid, marker='o', color='green', label='versicolor') plt.scatter(virginica_len, virginica_wid, marker='o', color='blue', label='virginica') plt.legend(loc='upper right') plt.title('Iris data') plt.xlabel('sepal length (cm)') plt.ylabel('sepal width (cm)') plt.show()

slide-23
SLIDE 23

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Legend locations

string code

'upper left'

2

'center left'

6

'lower left'

3

'upper center'

9

'center'

10

'lower center'

8 string code

'upper right'

1

'center right'

7

'lower right'

4

'right'

5

'best'

slide-24
SLIDE 24

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Plot annotations

Text labels and arrows using annotate() method Flexible specication of coordinates Keyword arrowprops : dict of arrow properties

width color

etc.

slide-25
SLIDE 25

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using annotate() for text

plt.annotate('setosa', xy=(5.0, 3.5)) plt.annotate('virginica', xy=(7.25, 3.5)) plt.annotate('versicolor', xy=(5.0, 2.0)) plt.show()

slide-26
SLIDE 26

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Options for annotate()

  • ption

description

s

text of label

xy

coordinates to annotate

xytext

coordinates of label

arrowprops

controls drawing of arrow

slide-27
SLIDE 27

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using annotate() for arrows

slide-28
SLIDE 28

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Using annotate() for arrows

plt.annotate('setosa', xy=(5.0, 3.5), xytext=(4.25, 4.0), arrowprops={'color':'red'}) plt.annotate('virginica', xy=(7.2, 3.6), xytext=(6.5, 4.0), arrowprops={'color':'blue'}) plt.annotate('versicolor', xy=(5.05, 1.95), xytext=(5.5, 1.75), arrowprops={'color':'green'}) plt.show()

slide-29
SLIDE 29

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

Working with plot styles

Style sheets in matplotlib Defaults for lines, points, backgrounds, etc. Switch styles globally with plt.style.use()

plt.style.available : list of styles

slide-30
SLIDE 30

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

ggplot style

import matplotlib.pyplot as plt plt.style.use('ggplot')

slide-31
SLIDE 31

INTRODUCTION TO DATA VISUALIZATION IN PYTHON

fivethirtyeight style

import matplotlib.pyplot as plt plt.style.use('fivethirtyeight')

slide-32
SLIDE 32

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON