Preparing your
gures to share with
others
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Ariel Rokem
Data Scientist
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()
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()
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Back to the default
plt.style.use("default")
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
The available styles
https://matplotlib.org/gallery/style_sheets/style_sheets_reference.ht
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()
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()
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 options
"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
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Practice choosing
the right style for
you!
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Sharing your
visualizations with
others
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Ariel Rokem
Data Scientist
A gure 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()
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Saving the gure to le
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
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Different le formats
fig.savefig("gold_medals.jpg")
fig.savefig("gold_medals.jpg", quality=50)
fig.savefig("gold_medals.svg")
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Resolution
fig.savefig("gold_medals.png", dpi=300)
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Size
fig.set_size_inches([5, 3])
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Another aspect ratio
fig.set_size_inches([3, 5])
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Practice saving your
visualizations!
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Automating gures
from data
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Ariel Rokem
Data Scientist
Why automate?
Ease and speed
Flexibility
Robustness
Reproducibility
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
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']
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"] == sport
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()
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Figure derived automatically from the data
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Practice automating
visualizations!
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Where to go next
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Ariel Rokem
Data Scientist
The Matplotlib gallery
https://matplotlib.org/gallery.html
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Gallery of examples
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Example page with code
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Plotting data in 3D
https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Visualizing images with pseudo-color
https://matplotlib.org/users/image_tutorial.html
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Animations
Image credit: Gytis Dudas and Andrew Rambaut
https://matplotlib.org/api/animation_api.html
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Using Matplotlib for geospatial data
https://scitools.org.uk/cartopy/docs/latest/
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)
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Seaborn example gallery
https://seaborn.pydata.org/examples/index.html
INTRODUCTION TO DATA VISUALIZATION WITH MATPLOTLIB
Good luck visualizing
your data!
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B