Matplotlib is support for several kinds of bar charts. In the most general case, we can plot a bar chart by sending in a parameter of X components and a parameter of the height of the bar. Let's do this now with a little bit of linear data. We bring in our regular libraries for pyplot and NumPy, then I just create our linear data. Now, I'm going to generate a list of the X labels. In this case I'm just going to have a whole bunch of numbers here just the exact same as the data, but you can make these whatever you want. Then we pass this with the linear data as the bar heights. We have the x values, these are going to be the actual values along the x-axis, the linear data is actually the height of the bar. In this case is going to be this increasing step size from 1-8. Then the width of the bar, I'm going to set that width to 0.3. We can see here that we get nice little blue bars, they start at zero and they go all the way up. We can see that the zeroth item is actually a number one in its height because the array that we made is one through eight. But then we use the range and NumPy arrange it looks at the size because we use this range function and so we did 0-7 for our x-axis labels. To add a second bar, we simply call this bar plot again with new data. Keeping in mind that we need to adjust the x-component to make up for the size of the first bar that we plotted. You'll get to feel my frustrations around this shortly. Let's get some new x values. Then I'm just going to make this exponential, I'm going to take our linear data and square it. Then for the new values, I'm going to add these items plus 0.03. I'm building in this little offset for each one of those. Then I just call two cost of bar, plot.bar. The first one is going to be our original data. The second one is going to be the new values, but I'm going to use the same width of 0.3 and the color is red. We see our linear data, and then we see our exponential data. The plot looks okay but not great. All of the X labels are to the far left of the items being described and not centered. Now, we can center them with a given bar using this align parameter, but it's a bit frustrating when you have to have multiple bars and then you have to calculate it all by yourself. It's a pain to go through and add new data series manually. This actually gets worse. We actually have to manually iterate through and adding dates if we want them as well, because otherwise we wouldn't be able to set the left bar location properly. I'm going to be honest, bar charts are one of my least favorite things to create in Matplotlib. I feel the foundation is there but in some of the ways I want to use the APIs, such as plotting several series of data in groups across time. These are sadly missing. There's some other nice features of bar charts though. For instance, you can add error bars, teach bar as well using this y error parameter. For example, each of the pieces of data in the linear data might actually be a mean value computed from many different observations. We can just create a list of error values that we want to render as well and show those as error bars. Let's take a look. I'm just going to import this random function rand int. It's going to generate random integers and then I'm going to use a list comprehension here. Now you'll see list comprehensions a lot in examples. It's good to make sure that you know how to read that syntax in Python. This says, when we use the indexing operator in this way, the square brackets it says create me a list. Inside is our algorithm, if you will, for the list. Call randint 1, 4 to create a random integer 1-4 for x in range, and then we set the size of our range here. This is going to plot now the error bar. We just have our x values, our linear data, just like before, our width. Then here we can just set the y error and to this linear error. That's going to be the numbers between one and four inclusively here. Let's take a look. That's nice. You can see actually in the example that I ran, and of course yours might be different because we're using random numbers. In the example I ran, one of the error bars actually goes beyond the bottom of our chart, the zero in the x-axis and that's fine. Matplotlib just adjusts things for us automatically. Now, we could do stacked bar charts as well. And this actually works better because you don't have to deal with all the PGD positioning arguments. For instance, if we wanted to show cumulative values while also keeping the series independent, we could do this by setting the bottom parameter of the second plot to be equal to the first set of data to plot. You plot one piece of data and then you plot the second piece and you can just use one parameter, set the bottom for each of those bars as the heights of the previous one. Here, I've got this range of values. I plot our first data, the linear data in here, I'm going to call it B. Then for the next set, the exponential data, I'm going to use this bottom keyword and set it to the linear data. Remember these linear data, these are the heights in order and the x vals are the first parameter. The x vals are the same for these two series. The linear value heights and the exponential heights are different and for the exponential data, I want their heights to start at the bottom of the linear data on an item-by-item basis. I'm going to make that one red. Finally, we can pivot this bar graph into a horizontal bar graph by calling the bar h function instead. But note that we've got to change the bottom to a left and the width to height. Our language changes as we pivot things to the side. To switch to horizontal bar charts, we just use bar h as the function, and now we just change our height to width or rather our widths to height here and we change our left to the linear data instead of the actual bottoms. That's a very basic stacked horizontal bar chart. Those are the basics of charting and plotting in Matplotlib. I think it's important to realize there really isn't any magic going on here. That's one thing I think you'll be able to take away from my lectures. The scripting layer is really just a set of convenience functions on top of this object layer. Some people have a preference for one or the other but at the core they manipulate the exact same data elements. I don't think you should think of them being as opposed in one way or duplicative, they work together and being able to move back and forth is really important. I would say that in general, a data scientist is going to be using that scripting layer regularly, it's certainly what I do. Now, we've talked about some fundamental charts in this module, scatter plots, line graphs, and bar charts. There are more and we're going to dig into some of those in the next module, more advanced ones. But I think these are a good base for consideration. The assignment for this module is a tough one. It's meant to show you real-world data visualization problems. You're going to have to leverage your knowledge of the Pandas library from the first course and couple it with your new knowledge of Matplotlib to come up with an elegant solution. But the final results should be a portfolio-ready piece. I'll see you in the forums for discussions and for help.