Learn how to set plot elements—axis, ticks, and grid.
- [Narrator] Now you're going to learn about defining plot elements and mat plot lib. Plot elements add context to your plot, so the plot effectively conveys meaning to its viewers. You set axis limits to make sure your chart is well fit to your data graphing. You set axis tick marks and plot grids to make it easier and faster for viewers to interpret your chart at a glance. You can use sub plots to visually compare changes in data values under different conditions, like in different seasons, different locations, or in different years.
Adding plot elements is an essential part of object oriented plotting. We discussed functional plotting in chapter 2.1. Now it's time to go over object oriented plotting. With this method, you generate a blank figure, and then populate that figure with an axis and plot elements. There are four simple steps to object oriented plotting. The first is to create a blank figure object. The second is to add axes to the object, then you generate a plot within the figure object, and lastly, you specify plotting and layout parameters for the plots within your figure.
Those are the plot elements we're going to discuss. One more thing I'm going to do in this demonstration is to show you how to generate sub plots. A sub plot is a plotting figure that contains more than one plot, or sub plots. It's easy to generate sub plots using mat plot lib. I'm about to show you how. Let's look at object oriented plotting and how to define plot elements in mat plot lib. In this demonstration, you're going to be using NumPy and Pandas, as usual. So we'll copy and paste these libraries in, and then you're also going to be using mat plot lib, so you want to import that like we did in the last video.
Import mat plot lib.pyplot as PLT, and then we're going to set our C params again. So we'll say from mat plot lib import rcP params. Okay, great, and we run this, and we've got our libraries, and then let's set our layout settings for all of the data visualizations within this Jupyter notebook.
We covered all this in the last section. We'll just run that and then we want to create some objects we can work with for the data visualization. So we'll just create a X variable and a Y variable. We're going to set X equal to a series of numbers between one and nine. So, we'll call the range function, and we'll pass in one through ten is the limit, and then we're also, for Y, we're going to make that a list and we'll just add some values. So, one, two, three, four, zero, four, three, two, one, and I'll go back through and add in the commas.
Next we want to create the blank figure object. We'll call it fig. So we're going to say fig is equal to PLT.figure. This generates a blank figure, and then we're going to add an axis to this figure, and we need to tell Python where to place that axis. To add the axis you call the add axis method. We'll say fig.add axes, and then we're going to create a list, and we're going to tell it where we want that axis placed. So I'll say, on the left side we want it placed at .1.
On the bottom, we want it placed at .1. Width, we want it one, and height we want one, and I'm just going to reuse this all throughout the rest of the course. This is going to be where we place our axes. And to generate a plot, call this whole thing ax, and that's our blank figure with an axis added to it. We call the plot function off of the X object, and pass in the name of the variables we want plotted.
So in this case, we're going to say ax.plot, and pass in X Y. You need to replace this period with a comma, and there we have it. We have a simple line plot we created through object oriented plotting method. Now I'm going to show you how to set axis limits, and tick mark labels. Each time we create a plot with object oriented plotting, we need to recreate the figure and add the axis. But this time let's set some limits of the X and Y axes. You do that with the set X lim or set Y lim methods.
So let's set the X axis limits to between one and nine. We do that by calling the set X lim method off of our ax object, set_xlim, and then we pass in the list with the limits we want to use, one and nine, and then again, we'll set our Y limits by calling the set Y lim method, and then this time we'll pass in a list with the limits of zero and five for the Y axis, and also I want to show you how to set tick marks for the X and Y axes.
You can do that with the set X ticks method, and the set Y ticks method. I'll show you here. We'll call set_X ticks and we're going to call that off of our ax object, and then we're going to tell Python that we want tick marks at position zero, one, two, four, five, six, eight, nine, ten. And I'll go back through and add the commas in real quick, and then let's do this again for our Y tick marks.
So I'm going to copy and paste this down. I change X to Y, and then this time we're going to ask for our Y tick marks to be a series of numbers between zero and five, and then to plot this whole thing, we write the name of our object, ax, and then we call the plot method off of it, and we pass in our two variables X and Y. This looks pretty good, so we'll print it out, and here we go. I made sure to make the tick marks on the X axis a little inconsistent, so you could see what Python's actually doing here, and when you call the X ticks method and pass in the list of the tick marks you want labeled, you get those and only those, as you can see here on the bottom.
One more thing I want to show you here is how to add a grid. We'll reuse the chart we just made. So I'm going to copy and paste the code we used to create it from above, and then all you have to do here is add ax.grid. You call the grid method off of your ax object to add a grid to your chart and then we'll plot it out. And you can see here how it makes it easier to read the chart at a glance.
That's the point of adding the grid. The last thing I wanted to show you in this demonstration is how to generate sub plots in mat plot lib. To create sub plots, you use mat plot lib sub plots method. When you pass in the number of rows and columns, mat plot lib will plot out several plots inside of one figure. To do this from scratch, you first generate a figure object, then you add an axis for plots that you want added within it. We want one row and two columns of sub plots, so we're going to write fig equals PLT.figure.
This generates our blank figure object, and then inside this figure we're going to have two axes this time, ax one and ax two, and we're going to tell Python that we want this to be a set of sub plots. So we're going to call the sub plots function, and we're going to pass in the number of rows and columns we want for the sub plots. So it's going to be one row, two columns, in the first plot that'll be axis one. Let's just plot X. So we'll call the plot method, then we'll pass in the X variable, and then in the second plot, ax two, we'll plot both X and Y.
So we'll say .plot pass in X and Y, and print that out, and there we have it. So we have got one figure object, two axes, and two separate plots plotted out. That was an easy walk through in how you can customize your mat plot lib data visualizations, and next I'm going to show you how you can format your plot with custom colors, widths, alignments, line styles, and marker types.
- Getting started with Jupyter Notebooks
- Visualizing data: basic charts, time series, and statistical plots
- Preparing for analysis: treating missing values and data transformation
- Data analysis basics: arithmetic, summary statistics, and correlation analysis
- Outlier analysis: univariate, multivariate, and linear projection methods
- Introduction to machine learning
- Basic machine learning methods: linear and logistic regression, Naïve Bayes
- Reducing dataset dimensionality with PCA
- Clustering and classification: k-means, hierarchical, and k-NN
- Simulating a social network with NetworkX
- Creating Plot.ly charts
- Scraping the web with Beautiful Soup