Plotting directl y u sing pandas P YTH ON FOR R U SE R S Daniel - - PowerPoint PPT Presentation

plotting directl y u sing pandas
SMART_READER_LITE
LIVE PREVIEW

Plotting directl y u sing pandas P YTH ON FOR R U SE R S Daniel - - PowerPoint PPT Presentation

Plotting directl y u sing pandas P YTH ON FOR R U SE R S Daniel Chen Instr u ctor Plotting in P y thon Q u ickl y sho w data pa erns Plo ing methods in P y thon : Pandas Seaborn Matplotlib PYTHON FOR R USERS Pandas plot method


slide-1
SLIDE 1

Plotting directly using pandas

P YTH ON FOR R U SE R S

Daniel Chen

Instructor

slide-2
SLIDE 2

PYTHON FOR R USERS

Plotting in Python

Quickly show data paerns Ploing methods in Python: Pandas Seaborn Matplotlib

slide-3
SLIDE 3

PYTHON FOR R USERS

Pandas plot method

plot() method

Works on the pandas DataFrame and Series

  • bjects

Pass plot the kind argument

kind of plots: line : line plot (default) bar : vertical bar plot barh : horizontal bar plot hist : histogram box : boxplot kde : Kernel Density

Estimation plot

density : same as ‘kde’ area : area plot pie : pie plot scatter : scaer plot hexbin : hexbin plot

slide-4
SLIDE 4

PYTHON FOR R USERS

Univariate: Histogram

import matplotlib.pyplot as plt iris['sepal_length'].plot(kind='hist') plt.show()

slide-5
SLIDE 5

PYTHON FOR R USERS

Univariate: Bar plot

cts = iris['species'].value_counts() cts.plot(kind='bar') plt.show()

slide-6
SLIDE 6

PYTHON FOR R USERS

Bivariate: Scatter plot

iris.plot(kind='scatter', x='Sepal.Length', y='Sepal.Width' plt.show()

slide-7
SLIDE 7

PYTHON FOR R USERS

Bivariate: Boxplots

iris.plot(kind='box') plt.show()

slide-8
SLIDE 8

PYTHON FOR R USERS

Bivariate: Boxplots

iris.boxplot(by='Species', column='Sepal.Length') plt.show()

slide-9
SLIDE 9

Let's practice!

P YTH ON FOR R U SE R S

slide-10
SLIDE 10

Seaborn

P YTH ON FOR R U SE R S

Daniel Chen

Instructor

slide-11
SLIDE 11

PYTHON FOR R USERS

Seaborn

barplots: barplot histograms: displot boxplot: boxplot scaer plot: regplot Seaborn benets colored points by data facet plots by data

slide-12
SLIDE 12

PYTHON FOR R USERS

Seaborn histograms

import seaborn as sns import matplotlib.pyplot as plt sns.distplot(iris['sepal_length']) plt.show()

slide-13
SLIDE 13

PYTHON FOR R USERS

Seaborn count plot

sns.countplot('species', data=iris) plt.show()

slide-14
SLIDE 14

PYTHON FOR R USERS

Seaborn boxplots

sns.boxplot(x='species', y='sepal_length', data=iris) plt.show()

slide-15
SLIDE 15

PYTHON FOR R USERS

Seaborn scatterplots

sns.regplot(x='sepal_length', y='sepal_width', data=iris) plt.show()

slide-16
SLIDE 16

PYTHON FOR R USERS

Seaborn scatterplots w/out regression line

sns.regplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False) plt.show()

slide-17
SLIDE 17

PYTHON FOR R USERS

Seaborn facets

sns.lmplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False, col='species') plt.show()

slide-18
SLIDE 18

PYTHON FOR R USERS

Seaborn FacetGrid

g = sns.FacetGrid(iris, col="species") g = g.map(plt.hist, "sepal_length") plt.show()

slide-19
SLIDE 19

Let's practice!

P YTH ON FOR R U SE R S

slide-20
SLIDE 20

Matplotlib

P YTH ON FOR R U SE R S

Daniel Chen

Instructor

slide-21
SLIDE 21

PYTHON FOR R USERS

Matplotlib plots

import matplotlib.pyplot as plt plt.hist(iris['sepal_length']) plt.show()

slide-22
SLIDE 22

PYTHON FOR R USERS

Matplotlib scatter

plt.scatter(iris['sepal_length'], iris['sepal_width']) plt.show()

slide-23
SLIDE 23

PYTHON FOR R USERS

Polishing up the figure

fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) ax.set_title('Sepal Length') ax.set_xlabel('Sepal Length') ax.set_ylabel('Sepal Width') plt.show()

slide-24
SLIDE 24

PYTHON FOR R USERS

Rotating axis ticks

fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) ax.set_title('Sepal Length') ax.set_xlabel('Sepal Length') ax.set_ylabel('Sepal Width') plt.xticks(rotation=45) # rotate the x-axis ticks plt.show()

slide-25
SLIDE 25

PYTHON FOR R USERS

Parts of a matplotlib figure

slide-26
SLIDE 26

PYTHON FOR R USERS

Parts of a matplotlib figure 2

slide-27
SLIDE 27

PYTHON FOR R USERS

Figures and axes

fig, ax = plt.subplots() ax.scatter(iris['sepal_length'], iris['sepal_width']) plt.show()

slide-28
SLIDE 28

PYTHON FOR R USERS

Multiple axes

fig, (ax1, ax2) = plt.subplots(1, 2) ax1.scatter(iris['sepal_length'], iris['sepal_width']) ax2.hist(iris['sepal_length']) plt.show()

slide-29
SLIDE 29

PYTHON FOR R USERS

Remember

fig, ax = plt.subplots() sns.regplot(x='sepal_length', y='sepal_width', data=iris, fit_reg=False, ax=ax) plt.show()

slide-30
SLIDE 30

PYTHON FOR R USERS

Clearing the figure

fig, (ax1, ax2) = plt.subplots(1, 2) ax1.scatter(iris['sepal_length'], iris['sepal_width']) ax2.hist(iris['sepal_length']) plt.show() plt.clf()

slide-31
SLIDE 31

Let's practice!

P YTH ON FOR R U SE R S