|
|
|
8:21 |
|
|
transcript
|
0:25 |
Have you ever struggled to build data visualizations in python? Are you confused by all the frameworks? Do you want to level up your ability to convey complex analysis through visualizations? Are you interested in building interactive dashboards? If so, then this course is for you. This is Chris Moffitt, and I am excited to offer this python data visualization course through Talk Python Training
|
|
|
transcript
|
0:53 |
Before we talk about using python to visualize data, let's take a step back and talk about why we want to visualize data. A good example of this is an Anscombe's Quartet. This is a data set that has four different unique sets of X Y. Data pairs. And if you were to run statistical analysis on each of these sets, you would find out that each set has very similar properties. The average the variance, the correlation between the X and Y variables and each data set is about the same. However, when you visualize the data, you can see that the properties of the data set are very different and this is a dramatic illustration of the importance of using data visualization in addition to standard statistical analysis tools.
|
|
|
transcript
|
1:00 |
So why might you choose to visualize your data? This list from Dr. Alexander Lex at the University of Utah from his data science course provides a lot of good examples that hopefully will inspire you as you embark on your data visualization journey, I frequently use many of the data visualization techniques I'm going to cover to answer questions, communicate ideas to others, test or reject hypotheses that I might have, for certain types of data Visualization is almost the only way to answer and reveal complex patterns in the data when I'm trying to record or present information. Maybe in a different context, visualization can be really handy, and for certain types of computational analysis, visualization is almost the only way to interpret the data. Finally, the most common use for visualizing the data that I've seen is to tell a story and we'll walk through some of those key considerations for visualizing data to effectively tell that story.
|
|
|
transcript
|
0:48 |
We've talked about why you might want to visualize data, but why use python to do it? Well, first python is one of the most popular programming languages in the world and is growing in popularity over time. I find also that Python is easy to learn. So if you're new to this space, the hurdles to get started are a lot lower. Python also works on all major operating systems so that you can run on your Linux your Mac or your Windows system. Python also supports many automation data analysis and visualization tasks. So this means that it will be a tool that you can use and grow with over time. And finally, one of the benefits of python is that there is a great collection of libraries that are available to do different visualization tasks. And here's a sample of the ones that we're going to cover in this course.
|
|
|
transcript
|
0:37 |
Python is a great tool for data analysis and visualization, but one of the frequent concerns is that newcomers have challenges navigating this complex ecosystem because there are so many options broadly, the options are broken into three groups. There's a Matplotlib group, a JavaScript based group, and an OpenGL based group. In this course, we'll focus on a handful of Javascript and Matplotlib based solutions and talk through some of the pros and cons and help you choose which one is gonna be the best tool for the types of analysis that you do.
|
|
|
transcript
|
0:54 |
Let me lay out the course objectives. At the end of the day, I want to give you experience with each of these visualization libraries so you can choose the one that best meets your needs. So the way we'll do this is we'll talk about some of the most common libraries that have a good balance of power and ease of use. I'll give you the basic knowledge you need to get started with each of these libraries Each one has its own unique API. And I'll familiarize you with some of the key ways that you want to use the API. To get the most out of your visualization needs. Then we'll go through some specific data analysis steps to get you some more experience with using the library. At the end of each section, I'll go through the pros and cons so that you can go in eyes wide open and choose a visualization tool that meets your specific needs.
|
|
|
transcript
|
1:21 |
Here, the topics we'll cover in this course first, I'll talk about some basic visualization concepts that will help you get the most out of each of the tools we talk about next. The first library will cover is matplotlib, which is the grandfather of many of the plotting libraries in python. And spending time understanding it will help you greatly as you progress in your data visualization capabilities. Pandas builds on top of matplotlib for using custom visualization on top of the data frame that you're already using to analyze your data. Seaborn is a very powerful tool for doing statistical analysis and visualization of your data. Then we'll transition to some of the Javascript based frameworks like Altair and Plotly, which produces very visually appealing and interactive charts. The final two sections will cover are related to building your own dashboards. So, Streamlit is a tool for combining any of the visualization libraries that you've already used to add more interactivity. And then at the end we'll talk through Plotly Dash framework, which provides a tremendous amount of customization and flexibility for building your own interactive dashboards.
|
|
|
transcript
|
1:06 |
This course assumes basic python and pandas knowledge to get the most out of the data visualization libraries will be discussing. You'll need to be able to install libraries on your system using pip or conda and once they're installed, be familiar and comfortable importing those modules. The pandas will use it to read in CSV and Excel files as well as group and aggregate data more generally from a python perspective, you should be comfortable using dictionaries and lists and assigning values to variables as well as using an object oriented interface. Finally, the code will be in Python 3.8 and using Jupyter notebooks. If you're not familiar with Python and Pandas and those examples, I just walked through. I recommend 'Python for the absolute beginner' in the talk python training series to get you up to speed on basic python skills. Once you're there, if you would like to learn more about pandas, I recommend my course Excel to Python at that point. You should have a strong foundation to really get the most out of this course.
|
|
|
transcript
|
0:23 |
To get the most out of this course, I encourage you to go out to Git Hub and download the materials for this course on your own system. Then through each chapter, as I walk through, examples, replicate that activity on your own system, explore the data and gain some experience with how to use these very powerful visualization tools.
|
|
|
transcript
|
0:54 |
Before we get started I'd like to introduce myself a little bit more and give you my background. As I mentioned, my name is Chris Moffitt. If you like to connect with me on twitter, I'm @chris1610. My email is 'chris@pbpython.com'. I've been using python for well over a dozen years in multiple business settings to solve real world business problems. I've also worked on the Excel to python course, which talks about how to take your business processes based in Excel and move them to a more scalable python and pandas solution. I am an instructor of DataCamp where I teach a course on data visualization with Seaborn and I blog at practical business python. Before we get started, I'd just like to say I'm really excited about this course and really appreciative of your willingness to spend a little bit of time with me and learn about data visualization in python.
|
|
|
|
9:13 |
|
|
transcript
|
0:47 |
If you're new to data visualization, you may be ready to jump right in and start doing some python coding. However, there is a surprising amount of research and science on how to most effectively visualize information. I'd like to go through a few core concepts that will make your visualizations much more effective. I also encourage you to take a look at some of the resources on this screen because there are many great books on this topic. And as you start to explore the various ways to use visualizations, more effective. Building out your knowledge, through books like this will help you be more effective in creating visualizations for your day to day analysis.
|
|
|
transcript
|
1:22 |
An important concept in data visualization is Aesthetics, Aesthetics describe every aspect of a graphical element on your visualization. A few common examples should make this clear. The actual position of an element on an XY. Axis is one aesthetic. Another common aesthetic is shape or size. To differentiate different elements. Color is a very important one that we'll talk about in future slide. The line width or line type can be useful in those types of charts where we have lines and as we apply aesthetics to the data, there's really only two types of data. So continuous data is data like time or weights or length or temperature, where there's a continuous range of values versus discrete data, such as count of, value of our dice, roll a yes or no answer. And in general most aesthetics can be used for continuous and discrete data, but shapes and lines usually work best for discrete data. We will use these concepts as we start to dive into each of the libraries that we're talking about because each library has a different way of mapping these aesthetics into your code.
|
|
|
transcript
|
0:52 |
We talked about differentiating data between continuous and discrete data. There's another way we can break it down. We can have quantitative data, which is numerical or qualitative data, which is sometimes called categorical data. So quantitative data describes the quantity of things. When we talk about the height, something might be 1.5 m, or weight could be 20 lbs. The price of an object could be $25, or time is measured in 60 seconds. Those are all quantitative numerical values. Qualitative data would be something like the hair color is red or blonde, nationality. US, a car as a sedan or a pickup and a pet as a dog or cat.
|
|
|
transcript
|
1:14 |
Now let's bring together continuous and discrete data and compare it to quantitative and qualitative data and give some specific examples. We have quantitative or numerical data that's discreet. It would look like a set of numbers like 1234 or specific dates. We could also have discrete categorical values and those can be broken down into an Unordered list like the pet list of dog, lizard or monkey where there's really no difference between them or an ordered list. Or there is some concept where there is a relationship between the order of the items in the list. If we look at continuous numerical values, those will be arbitrary numbers such as 5.5 a percentage or maybe currency as well as time that includes hours and minutes. The reason I cover this is that your visualization is going to be more effective if you treat for example an ordered qualitative value differently than a continuous quantitative value. And in fact some of the modules that we'll talk about can infer types of visualization based on the data type of your pandas data frame.
|
|
|
transcript
|
1:33 |
A very important aesthetic that we talked about earlier is color. Sometimes it's referred to as Hue and some of the libraries will be discussing. When we think about color, it's common to have a discrete color scale. In this case we have a categorical scale with a small number of swatches that are chosen to be representative of some sort of data set. We can also have a sequential discrete scale which goes from either dark to light or light to dark and then also a diverging scale, which means on each ends, it tends to be darker and as you get towards the middle, the colors converge in are harder to distinguish. A continuous color scale. On the other end has a nearly infinite number of colors It can also be sequential, which means it goes from dark to light or light to dark. It can also be diverging similar to the discrete color scale that we talked about where the middle is harder to distinguish but as the edges you can start to see more differentiation. One other item I want to mention as we talk about colors and start to apply this towards our visualization is to keep in mind that a large percentage of the population including yours truly do have some version of color deficient vision and that you can use these tools to choose palettes that will work well for people that have certain types of color blindness. So I encourage you to keep that in mind as you start to choose colors for your visualiztion.
|
|
|
transcript
|
1:02 |
If you're somewhat new to this space, I want to talk about one other topic that's really powerful and useful in the tools that will be covering. And that's the concept of small multiple plots. And this is basically just a way of putting a whole bunch of small graphs or charts together in one place using similar axes and scales so that you can identify trends This example from Seaborn shows how you can quickly look at this data and identify some of the outliers because there are so many charts condensed into a small space. This term is sometimes called a trellis chart, a lattice chart, a grid chart, panel chart or facet grid. So as you can see, there's a lot of different terms, but each of the visualization libraries that we're gonna talk about allows us to do this and I wanted to raise the concept now so that as we start to dive into the modules, you have some exposure to it and understand what it is. We're trying to accomplish with these types of charts.
|
|
|
transcript
|
1:15 |
I want to talk a little bit about the types of analysis that I typically do when I'm doing data visualization. The first type when I have a new data set is to do exploratory analysis. An exploratory analysis is characterized by the process of getting familiar with the data where the focus is on speed and doing multiple visualization types. I do this to find interesting nuggets and then dive deeper into the data. This is also typically something that I do as an individual. Once I have found the information that I want to convey to someone, I'll start to do more explanatory analysis. And what I mean by that is that this is focusing on communicating findings to an audience. The focus here is not on speed, it's on clearly conveying the message to that audience. I will spend time crafting the visualization and turning it into a standard report and frequently what I find is one tool might be good for exploratory analysis but a different tool Once I understand what I'm trying to say is a good tool for explanatory analysis and this is a good distinction for you to keep in mind as you start to use some of the tools we are gonna talk about.
|
|
|
transcript
|
1:08 |
We've talked about data visualization quite a bit but I don't want to lose sight of the fact that a lot of data visualization should really be a part of working with your data. And I thought this quote from Mike Bostock really drove that home. I want to give one specific example of tidy data versus wide data to hammer this concept home. When we talk about tidy data, I mean data in the example that is one line has all the complete information. It's like a record in a database. So in this case for the amazon sales data we have the name of the book the author and the user rating and some other information by year we can transform that data into a wide data set using the pivot table function. So here we have the fiction and nonfiction reviews by year in the wide data format My point with all of this is that when we're doing data visualization, you need to be prepared and comfortable using tools like groupby, pivot table, or melt to get the data transformed in a way that is most effective for the visualization tool.
|
|
|
|
56:41 |
|
|
transcript
|
0:29 |
In the past two chapters, we covered some important data visualization background. Now we will start to actually code in python. We'll start this journey with the most mature python visualization library matplotlib. Now matplotlib does have a bit of a reputation of being too complex or difficult to learn. However, I think with some basic concepts, you can learn matplotlib and start to incorporate it into your own data visualizations
|
|
|
transcript
|
0:59 |
As I mentioned, Matplotlib has been around for a long time. The first release was in 2003 and it was actually heavily influenced by MATLAB. John Hunter laid out some of these core tenets when he created matplotlib, he wanted a python plotting package that would generate output that was publication quality, so it had to really look good and generate in multiple formats. He also wanted an environment so that you can embed a graphical user interface for more rapid application development. He wanted code that was easy enough that he could understand it extend it. At the end of the day, he wanted making plots to be easy and I think the greatest testament to matplotlib is that it has been around for so long and that it is used as a foundation for so many of the plotting libraries and the data visualization libraries that we use in Python today.
|
|
|
transcript
|
0:47 |
Let's take a look at the landscape again and focus on what matplotlib does So, it is a foundational library for many of the visualization tools in the python ecosystem, and two of them that we will talk about in future chapters are Pandas and Seaborn. This chapter will focus on using matplotlib on its own because it is very powerful and can do a lot of visualization. The other key takeaway here is that matplotlib. If you understand it, then you can really get the most out of pandas and Seaborn in some of these other libraries, so it's well worth your time to understand that matplotlib and figure out how you can use it in your own data visualizations.
|
|
|
transcript
|
2:38 |
I'm a firm believer that the best way to learn this content is to follow along on your own system. So I'm going to discuss how to get your system set up so that you can experiment with some of the code on your own. I assume you have at least a little bit of familiarity with the Python package index or Conda and some of the other tools for installing modules on your system and managing environments. So one thing that I want to make sure you do is have a virtual environment or a conda environment set up so that the content that we're gonna walk through is separate from the other environments that you might have on your system. For this course, I'm using Python 3.8 but I don't use anything that is too specific to that version. So anything probably from a python 3.6 up to as of this recording 3.10 is coming out soon, should work. So as long as you're using a modern version of Python3 you should be good for most of the modules you can use pip or conda for installing I'm gonna use pip for the majority of the examples because I think that's a little more universal than Conda and for each chapter, I'll walk through how to install those modules. So for this chapter we're gonna focus on getting pandas, matplotlib and the Jupyter notebook installed and then I am gonna use stats models to show how to do a regression line and plot that with matplotlib, I did run into some issues and have in the past when installing on Windows. Sometimes Pywin 32 can be a little challenging to install with pip So, if you do have issues, I recommend using conda for some of these Binaries. like PyWin 32. So you can install doing conda installed pywin32 and then in future chapters, we're gonna install some additional modules that you will need for the visualizations during each chapter. I'll walk through this. But if you are a little more advanced and want to take a look at installing these on your own, you can but for now, just focus on pandas, matplotlib notebook and stats models. All the code I'm going to run through is in a Jupyter notebook. Towards the end of the course, I will be generating some code in VS Code. Finally, if you have any errors getting these modules installed or getting your environment set up, I highly encourage you to look at the individual package documentation because that will have the most recent information and tips and tricks we're getting these modules set up on your system.
|
|
|
transcript
|
1:50 |
Let's face it. Real world data is typically messy and I wanted the data in this course to mirror what you're gonna encounter once you apply these visualization concepts on your own. I've chosen to use data from the US. Department of Energy, fueleconomy.gov at this URL. I've downloaded the data and created a file called EPA_fuel_economy.csv Here's an example of the data that is in this file. The first seven columns include basic information about each vehicle per year. So you have the make model and year as well as the number of cylinders in the engine, the type of transmission, the engine displacement and the vehicle class. The C02 column is a measure of the estimated emissions of CO2. on an annual basis, barrels 08 indicates the number of barrels of oil per year to operate the vehicle and then what that cost would be on an annual basis We also include the different fuel type used for this estimate as well as the MPG, both highway city and combined. So I like this data set for a lot of different reasons. It has a large number of values, 24,000 values from 2000 to 2020, which means it's big enough that visualization is really going to help us understand the large data set. It's already in the tidy format. It has a mix of qualitative and quantitative variables and the variables are ordered and un ordered as well as discrete and continuous. So those concepts that we talked about earlier are going to apply. And then this is an area where we all have experience with vehicles. And hopefully it's interesting enough that you might choose to explore it on your own and see how it applies the vehicles that you own or operate.
|
|
|
transcript
|
1:08 |
One of the confusing concepts for new users to matplotlib is how the figure API interacts with the axes API. So an axes actually represents a single plot, whereas a figure is the broader container for one or more axes. This example from the matplotlib, documentation helps put it in context. So a lot of the things you think of with a figure makes sense. There's a title, there's a legend. We can have grids, we can have spines as well as in this example, a line or scatter plot. We also have X axis and Y axis labels and ticks. And those intuitively makes sense. But what is not clear to the new user is that this plot is actually an axis and that the figure is a container for one or more axes. And this concept is really important and I think we'll drive it home as we go through some more examples.
|
|
|
transcript
|
1:41 |
The second concept I want to talk about that can be really confusing for new mat- plot lib users is the fact that there are actually two interfaces to generating your visualization So the first one is a pyplot or a functional state based interface. And this is based off a matlab and it's designed for simple interactive plots and it relies on pyplot to automatically create and manage the figures and axes that we talked about in the previous section. The other approach is the object oriented approach where you create your figures and axes and then call methods on them to update them. Here's an example using pyplot of generating a simple histogram where you can see that the plot keeps track of the current figure and axes and just updates it with these commands. Whereas the object oriented approach, you create the figure in the axis using the subplots function. Then you update that axis with the histogram, your x labels your y labels, titles and then show that overall figure pyplot is around for that MATLAB experience and has been around for a long time. So a lot of the examples you're going to see online will be in the py plot format but you should try and translate it internally into the object oriented approach, for this course I will focus on the object oriented approach because that gives you the most flexibility and the most ability to update and interact with some of the other libraries they were going to be discussing.
|
|
|
transcript
|
1:13 |
I'm gonna go through a quick example of launching my Jupyter notebook environment. I'm doing this on a Windows system and I already have the terminal set up to boot into a conda environment. As you can see, I have several environments set up on my system. I'm going to use the data of this environment for this course. I'm already in the notebooks directory so this will launch the notebook and then open up a browser with my environment and this is what my base environment looks like. Want to walk through the data directory that I have where I've placed three files that would be working through this course, there's an amazon book, Excel file, the EPA fuel economy CSV. File that we talked about and I made a summary file for the EPA_fuel economy that I'll use in some of the future exercises So this is a basic environment. I'll work through for the first couple of chapters and then at the end we'll use VS Code and I'll walk through how to use that a little bit later.
|
|
|
transcript
|
2:03 |
Now I'm going to create a new notebook to capture the information for our first exercise First thing I'm going to do is rename this notebook and I prefix it with the 02 just to do some of the ordering. Next I'm going to bring in all of my imports. Those are the standard for the path and pandas and numpy. Now I'm going to do 2 imports from matplot Lib. Plot is the standard starting point for creating all of our visualizations and a little bit later I'm going to show how to customize the ticker using this function from matplotlib. Next let's get our directory set up so we can read in the files. If you're not familiar with pathlib I'll give a quick overview. This is saying that our source file is in our current working directory under the data Raw subdirectories and files. EPA_fuel_economy.CSV. I'm also going to set up an image directory that I will use to store some of the plots a little bit later. And now here's the data frame that we talked about earlier so you can see at the top part of the data shows the first five records in this data set. I would like to do info to see a little bit more information about the data as well so we can see have a really good overview of the data and now we start to plot some data.
|
|
|
transcript
|
2:13 |
Now that we have the data loaded into our data frame. Let's do a really simple histogram plot before you plot something in a jupyter notebook. Sometimes you may need to use a magic command to tell it that you're plotting with matplotlib. Now in more recent versions of notebooks, you may not have to do this, but I want to point it out because you're going to see this a lot in online documentation. So now we've told the notebook that we're going to plot a matplotlib plot. Let's do a very simple histogram and I like using histograms because it's just one variable that we're looking at. In this case, we're going to plot a histogram of the combined fuel economy for all of the values. Now, one of the things you'll notice is that the Histogram is fairly straightforward, but you've got all this other information that is getting returned and a lot of times you're not gonna want to see that all the time. So there's a little trick you can do if you add that semicolon at the end it will suppress that information. So sometimes I will be doing that in the course. And what I'm showing you as an example of the state based interface using pyplot that I talked about that we don't want to use. So I'm going to go through that example just a little bit more detail so you can see how it works and I'll compare it and contrast with the object oriented interface So let me show how to customize the plot using the pyplot interface. So I've expand the example so that the plot has more information about what's going on So I continue to do the histogram. Then I labeled the X and Y axis with the number of cars and the combined fuel economy. I added a title and then I used plot.show to make sure that the final visualization is shown. Now we will go through the object oriented api and show how that works.
|
|
|
transcript
|
4:47 |
I wanted to highlight a couple of changes I made to the notebook just to indicate the difference between the two interfaces that we've been talking about. So I have updated the notebook. Out of the gate, say this is the state based interface that we talked about. And then down here is the object oriented interface, which is what we recommend. And you can always do a kernel restart and run all, get us back to the same spot. Now I'll show how to actually use the object oriented API. So now we have the same histogram that we did before, but instead of doing plot.hist, we did ax.hist. And on the surface it looks like we didn't really accomplish a whole lot, but by creating the figure and the axes we have a lot more control over it and it's a lot more consistent pythonic, API and we'll walk through some more examples of that. The other thing I wanted to talk about remember we did matplotlib inline appear so that the figures would automatically display. There is another approach I wanted to call out called matplotlib notebook. This is going to give a more interactive example and I'm gonna walk through and show it so that you're aware of it. I personally don't use it very often, but I think it is helpful to see. So I've enabled this notebook interface and I'm gonna do a little more complex example where we will let's copy and paste that. So we don't have to re type everything. So we'll create that histogram. But now we want to set the X Label, the Y Label and the title. Then we'll show the figure. Let me code that for you. So now what we've done is we've established that axis put the histogram based on the combined 08 column that we've been using set the X label and the Y label and title on that axis. And then showing this interactive figure that you can move and adjust and different plot types is maybe a little more useful than others. And then when you're done interacting with it, you can turn it off. Like I said, I don't tend to actually use this format very often I'm going to convert back to using matplotlib in line. I'm also going to comment this out because sometimes it gets a little confused when you make multiple changes in the same notebook, we start and run it all again. So when I ran it all again, I got this warning here because I have disabled matplotlib notebook. The fig.show it doesn't like that so I can rerun it without that, it will automatically display and everything's okay. So just wanted to kind of walk through that a little bit more. So let's give another example of using the object oriented interface where we have a different approach to setting the X Label, the Y label and the title. So it's going to start the same way. So we defined our figure to find the axis object, put the histogram on that axis and then instead of setting the doing three separate lines, we can use ax.set and pass X label, Y label and title as parameters to it. And now we have the same histogram with x label, Y label, and title Set. But we have used a slightly different API. To do this and one pointed out because you'll see examples of both and it's a little bit of personal preference. But I do think using Set is a little more easy to understand and grasp as you're getting started with that matplotlib and just a reminder. If you want to get rid of the extra text. We had that semi colon. Rerun it and we have our plot So I'm going to restart and run all again.
|
|
|
transcript
|
3:35 |
Now that we've talked a little bit about how do you see the object oriented interface I wanna take a step back and talk about how we can also customize the plots. So the histogram that we've been working with, you might have noticed that the data is skewed quite a bit and maybe we want to dive in a little bit deeper on a specific range and we can do that So let me show you an example of customizing the range. We can pass, By passing the range of 10-50, we can tell the Histogram to only start at 10 And to go all the way up to 50. And this gives us a little bit more ability to focus in on the data. And it is pretty common operation you're gonna want to do with histograms The other things you can do, continue to copy and paste is try something called accumulative histogram. You can see there's a very different view here, we're still in the 10-50 range. But what it's telling us is When we get up until up to this 25-30 range that's where the vast majority of the cars are. So it's a kind of a different way to interpret the histogram data that we have been looking at. And another option we can do is just by continuing to change the parameters. We have a whole lot of different ability to analyze the data very quickly. So now we have, instead of having that filled in histogram we have the step function and have made it a horizontal histogram. And what I think is really interesting about this. And the reason I wanted to go through this is to explain to you that there are many parameters for changing the way that you look at the data in mat plot lib. And so it's important to look at the documentation, understand what those options are and figure out what works best for your own visualization outside of controlling the range. Probably one of the most common things that I do with histogram is you want to change the number of bins. So here we told it that there should be five bins between 10 and 50. Instead of letting matplotlib, figure it out automatically for you, you can specify it like I did there and to see the difference, it's really bump it up to maybe 100 bins can see a much more fidelity in your data. I don't think I want to talk about is why we're using the semicolon and what is actually returned from a histogram. Let's just leave it to the default number of bins. And let's say we actually want to know what the bins are. So the way we would do that run that command. So we get our same histogram. But if we look at the variable in it's an array of the number of values in each of the buckets or bins. You want to see the bins, you can look at the bins variable and you get that array and then the final one that I'm not gonna talk about much is patches, which are the actual bars and in more advanced uses of matplotlib. This is where you could do some additional customization if you wanted to, but I'm not going to go into that.
|
|
|
transcript
|
5:35 |
Now that we've talked about, how to use the object oriented interface to spend a little bit of time actually talking about how to work with figures and axes to plot multiple plots. For the first example we're going to create two plots and show how we display them together. So first we'll use this command to create a figure and with two axes. And if we want to access each axis, Mhm. Put a histogram on each one, and for the second one, just to show an example, we're going to create a second, histogram with a larger range. Put a semi colon on there, so nothing else displays. And now you can see that we have two histograms in one figure. So one is on axes, zero second, one is on axes. One we've got a histogram using the commands that we've discussed before. Now this approach of ax, If you look at what ax is, it's an array. And what I actually prefer to do is a different approach to make it a little more explicit. So I'll do everything else the same. And instead of accessing it through a list or a NumPy array, We've now assigned a variable. ax1 and I'm sorry, ax2. Mhm. If we run it, we get the same plot. Now this in and of itself isn't that useful, but it shows the concept. Another example that would make it a little more interesting is if we combined a box plot with a histogram. So let me show you how to make a box plot first. So here's an example now of the box plot and way to generate it is very similar to what we do for a histogram, you call the box plot function on the axes, set the title and the y label. And now we have a box plot. One of the things I don't like about this box plot is that it's showing all these outlier values. So one of the things I'm going to do is remove those and there's a parameter called show fliers. I set that to false. Then I have a little more consistent box plot that makes the data easier to to read because we have a much smaller scale. So now let's combine the two. Maybe I'm gonna copy a little bit of code here, just two. And while I'm at it I'm going to set some values so it's a little easier to read. And I'm also going to label the box plot. The final thing I'm gonna do to make this look a little bit better is I'm gonna set vertical equals false. So it will show horizontally and we'll add the labels just to make sure it's nice and clean. And there we go. Now we have two plots. So the figure contains ax1 and ax2. ax1 is a histogram, ax2 is a box plot. So we've talked about axes but we haven't talked about a figure yet. So let's show an example of why the figure can be useful So I'm gonna copy everything and after all the labels, I'm gonna actually label the figure. And we have other options. We can configure such as the font size and I'm also gonna make it bold. There we go. So now we have the MPG Distribution and vehicle MPG. At the top and this is all one image, which is really handy. The next thing I'm going to show is how we can have a little more control over actually how we create the two different axes. one way to do this so we can specify the number of rows, the number of columns. And I'm also going to specify the figure size. So what this will do is create a figure that will have one row and two columns. The figure size is nine x 4". So now we have a very different plot. So the histogram and box plot are side by side and maybe in this case we don't need the vertical there. So we have a nice representation of the MPG and distribution two different ways so that
|
|
|
transcript
|
1:52 |
One area where matplotlib really shines is the ability to save images in multiple different formats and we can use the figure that we just created to save our image. Earlier we defined an image directory |