Learn how to generate graphs directly with matplotlib. You can create line plots, show legends, create an array of plots and learn figures.
- [Instructor] Most of the times, we'll use Pandas plotting, but sometimes, we'd like more control over the finer details of how a chart looks. For this, we'll work with matplotlib directly. Let's open a new notebook and call this one mpl. And we'll write the usual import lines. First the magic matplotlib inline so it will draw on the notebook. Then, import NumPy as np and import matplotlib.pyplot as plt.
Now, let's create some data. Xs is NumPy dot Linear Space from minus six to six with 100 points, and ys is np.sinc of xs. And now, let's plot. Plt.plot xs and ys. Note that you don't have a legend here. By default, matplotlib won't generate legends. So let's create a plot with a legend. Plt.plot xs and ys, and now we need to give the line a label.
So the label is frac of sine of X, and X. And now, we'll say plt.legend. We have to give each plotted line a label, so matplotlib will know how to show the legend. We can use latitude to get the formula. The R in front of the label means it's a raw string, where the backslash don't mean anything special. We also use plt.legend to draw the legend. By default, plt functions work on the latest generated plot.
We can plot more than one plot on the same chart. So let's copy the code from above, and add another plot. We'll add plt.plot xs, and np.sine of xs, and the label is sine of X. And now I have two lines on the same plot. We can plot two plots side-by-side. The subplot command lets us specify the topology, how many rows and columns we'd like. We're going to have two rows with a single column, meaning one plot on the top of the other.
So first we say, we want plt.subplots two and one, and you want the first. And then plt.plot xs, N P dot since of xs, and the label is, we'll tell plt to generate legend on this one, plt.legend. Now we'll get the second plot, plt.subplot, two and one is the same topology, but now we want the second chart. And then, we'll use plt.fill, with xs, minus xs to the power of two, and the label is, again in LaTeX, minus x to the power of two.
And let's do a legend here as well. And now we've got two charts showing. In matplotlib, figure is the image that holds several charts together, and each chart is called xs. Matplotlib has many other options, and the documentation is very extensive. I think this will give you enough to start it.
- Working with Jupyter notebooks
- Using code cells
- Extensions to the Python language
- Markdown cells
- Editing notebooks
- NumPy basics
- Broadcasting, array operations, and ufuncs
- Folium and Geo
- Machine learning with scikit-learn
- Plotting with matplotlib and bokeh
- Branching into Numba, Cython, deep learning, and NLP
Skill Level Intermediate
NumPy Data Science Essential Trainingwith Charles Kelly3h 54m Intermediate
1. Scientific Python Overview
2. The Jupyter Notebook
3. NumPy Basics
Manage environments5m 11s
6. Folium and Geo
7. NY Taxi Data
10. Other Packages
11. Development Process
Next steps1m 33s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.