Summary
Summary
In this chapter, you learned how to create plots using Python and Matplotlib. Matplotlib and Python can be used to build a variety of plot types. Types of charts built with Matplotlib in this chapter:
plot type | function call |
---|---|
line plots | plt.plot(x,y) |
muiltline plots | plt.plot(x1,y1,x2,y2) |
bar graphs | plt.bar([locations],[sizes]) |
pie charts | plt.pie([sizes]) |
histograms | plt.hist(data) |
scatter plots | plt.scatter |
Each of the Matplotlib plot types accepts a different set of positional arguments and keyword arguments. Positional arguments must be passed in order, keyword arguments must be passed including the keyword, and an equals sign. | |
### plt.plot() functions |
plot type | matplotlib function | example |
---|---|---|
line plot | plt.plot() |
plt.plot([1, 4, 5]) |
multiline plot | plt.plot() |
plt.plot(x1,y1,x2,y2) |
bar chart | plt.bar() |
plt.bar(x_pos, heights) |
pie chart | plt.pie() |
plt.pie(slices_list) |
histogram | plt.hist() |
plt.hist(x, 20, density=True, histtype='bar') |
scatter plot | plt.scatter() |
plt.scatter(x, y, s=area, c=colors, alpha=0.5) |
The general outline of building a plot with Matplotlib starts with an import matplotlib.pyplot as plt line and ends with a plt.show() line. |
This general outline is shown below:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
<prepare data>
<call plot with data and keyword arguments>
plt.show()
Key Terms and Concepts
plot
graph
histogram
scatter plot
error bars
annotation
arguments
keyword arguments
positional arguments
function
method
object
array
matrix
list
data structure
homogeneous array
heterogeneous array
dimension
tick labels
Additional Resources
Matplotlib official documentation:
Matplotlib summary notebook on Kaggle:
https://www.kaggle.com/grroverpr/matplotlib-plotting-guide/notebook
Python Plotting With Matplotlib (Guide) on Real Python:
https://realpython.com/python-matplotlib-guide/#why-can-matplotlib-be-confusing
Python For Data Science: Matplotlib Cheat Sheet from DataCamp:
https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf