Preparing y o u r fig u res to share w ith others IN TR OD U C TION - - PowerPoint PPT Presentation

preparing y o u r fig u res to share w ith others
SMART_READER_LITE
LIVE PREVIEW

Preparing y o u r fig u res to share w ith others IN TR OD U C TION - - PowerPoint PPT Presentation

Preparing y o u r fig u res to share w ith others IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB Ariel Rokem Data Scientist Changing plot st y le import matplotlib.pyplot as plt fig, ax = plt.subplots()


slide-1
SLIDE 1

Preparing your figures to share with

  • thers

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

Ariel Rokem

Data Scientist

slide-2
SLIDE 2

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Changing plot style

import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL" ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

slide-3
SLIDE 3

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Choosing a style

plt.style.use("ggplot") fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL" ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

slide-4
SLIDE 4

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Back to the default

plt.style.use("default")

slide-5
SLIDE 5

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

The available styles

hps://matplotlib.org/gallery/style_sheets/style_sheets_refere

slide-6
SLIDE 6

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

The "bmh" style

plt.style.use("bmh") fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL" ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

slide-7
SLIDE 7

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Seaborn styles

plt.style.use("seaborn-colorblind") fig, ax = plt.subplots() ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL" ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"]) ax.set_xlabel("Time (months)") ax.set_ylabel("Average temperature (Fahrenheit degrees)") plt.show()

slide-8
SLIDE 8

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Guidelines for choosing plotting style

Dark backgrounds are usually less visible If color is important, consider choosing colorblind-friendly

  • ptions

"seaborn-colorblind" or "tableau-colorblind10" If you think that someone will want to print your gure, use less ink If it will be printed in black-and-white, use the "grayscale" style

slide-9
SLIDE 9

Practice choosing the right style for you!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

slide-10
SLIDE 10

Sharing your visualizations with

  • thers

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

Ariel Rokem

Data Scientist

slide-11
SLIDE 11

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

A figure to share

fig, ax = plt.subplots() ax.bar(medals.index, medals["Gold"]) ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals") plt.show()

slide-12
SLIDE 12

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Saving the figure to file

fig, ax = plt.subplots() ax.bar(medals.index, medals["Gold"]) ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals") fig.savefig("gold_medals.png") ls gold_medals.png

slide-13
SLIDE 13

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Different file formats

fig.savefig("gold_medals.jpg") fig.savefig("gold_medals.jpg", quality=50) fig.savefig("gold_medals.svg")

slide-14
SLIDE 14

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Resolution

fig.savefig("gold_medals.png", dpi=300)

slide-15
SLIDE 15

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Size

fig.set_size_inches([5, 3])

slide-16
SLIDE 16

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Another aspect ratio

fig.set_size_inches([3, 5])

slide-17
SLIDE 17

Practice saving your visualizations!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

slide-18
SLIDE 18

Automating figures from data

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

Ariel Rokem

Data Scientist

slide-19
SLIDE 19

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Why automate?

Ease and speed Flexibility Robustness Reproducibility

slide-20
SLIDE 20

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

How many different kinds of data?

summer_2016_medals["Sport"] ID 62 Rowing 65 Taekwondo 73 Handball ... 134759 Handball 135132 Volleyball 135205 Boxing Name: Sport, Length: 976, dtype: object

slide-21
SLIDE 21

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Getting unique values of a column

sports = summer_2016_medals["Sport"].unique() print(sports) ['Rowing' 'Taekwondo' 'Handball' 'Wrestling' 'Gymnastics' 'Swimming' 'Basketball' 'Boxing' 'Volleyball' 'Athletics']

slide-22
SLIDE 22

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Bar-chart of heights for all sports

fig, ax = plt.subplots() for sport in sports: sport_df = summer_2016_medals[summer_2016_medals["Sport"] == spor ax.bar(sport, sport_df["Height"].mean(), yerr=sport_df["Height"].std()) ax.set_ylabel("Height (cm)") ax.set_xticklabels(sports, rotation=90) plt.show()

slide-23
SLIDE 23

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Figure derived automatically from the data

slide-24
SLIDE 24

Practice automating visualizations!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

slide-25
SLIDE 25

Where to go next

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB

Ariel Rokem

Data Scientist

slide-26
SLIDE 26

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

The Matplotlib gallery

hps://matplotlib.org/gallery.html

slide-27
SLIDE 27

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Gallery of examples

slide-28
SLIDE 28

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Example page with code

slide-29
SLIDE 29

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Plotting data in 3D

hps://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

slide-30
SLIDE 30

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Visualizing images with pseudo-color

hps://matplotlib.org/users/image_tutorial.html

slide-31
SLIDE 31

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Animations

Image credit: Gytis Dudas and Andrew Rambaut hps://matplotlib.org/api/animation_api.html

slide-32
SLIDE 32

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Using Matplotlib for geospatial data

hps://scitools.org.uk/cartopy/docs/latest/

slide-33
SLIDE 33

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Pandas + Matplotlib = Seaborn

seaborn.relplot(x="horsepower", y="mpg", hue="origin", size="weight sizes=(40, 400), alpha=.5, palette="muted", height=6, data=mpg)

slide-34
SLIDE 34

INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB

Seaborn example gallery

hps://seaborn.pydata.org/examples/index.html

slide-35
SLIDE 35

Good luck visualizing your data!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH MATP L OTL IB