From the course: Data Visualization with Python in Excel

Understanding the basics of Matplotlib

- [Instructor] Great job earning your proverbial driver's license in the first chapter by understanding the rules of the road for data visualization with Python and Excel. The next three chapters serve as a grand tour to explore three packages available for Python and Excel at the time of this recording, highlighting the unique advantages of each. To start we'll examine Matplotlib, the oldest and most popular Python data visualization package included in Excel, and most widely used in Python overall. Let's see this in action. Open understand-matplotlib to follow along. The first plot we'll build will be a blank canvas using Matplotlib. We define the figure and axis here with fig and ax respectively, but haven't yet mapped any plots into this grid yet. Next, let's add some numbers by creating two lists, X and Y, and mapping these numbers into the placeholder grid, along with a label. Ctrl + Enter. Great work. We now have a plot. However, without labels, it's unclear what this plot represents. To address this, we'll add labels by applying a few more methods to ax. We'll add text to the title and axis labels, allowing us to understand what this chart is about. You'll see here that I'll add a title, labels, and a legend. Ctrl + Enter. Up to this point, we focused on generating a single plot at a time, but the true purpose of this architecture is to create multiple subplots. Let's explore how to do that now. We'll create some basic data again, and then a two by two grid by passing two, two to subplots and increase the figure size to provide more room for the plots. This will be a new plot, so I will add a new cell here. Next, we'll create some basic charts. Using zero-based indexing and Python, I will set up each plot to see what we've built here. In the first plot, we have a line chart. We then continue with bar, scatter, and pie charts respectively. You'll see that the generic plot function produces a line chart, and we'll use other functions to create different chart types. Ctrl + Enter to run, and let's show on our grid, Ctrl + Shift + M. It's pretty cool that we are almost able to build a mini dashboard here with just a bit of code, right? I hope you're starting to see the power of Matplotlib. While it can feel very code-heavy and regimented, there's so much you can do with it. Here's another example that might be more relevant to you. So far For simplicity, we've been plotting data stored in Python lists, which isn't typical when using Python in Excel. You're more likely to use Pandas DataFrames. Let's look at a simple example in the sales_df worksheet. We'll read the data from Python in Excel into a new data frame as sales_df. This data frame will be the basis to plot a line chart. Fortunately, if you're making a one-off typical chart in Pandas like this, you can leverage Matplotlib to make this super easy. We don't need to set up the fig and ax variables. All we'll need is this. In short, we just use the plot function to define what goes on each axis and set the chart title. Now we can run it. Amazing. That was much easier code to build. If I wanted to do some basic customization, I could add another argument to make this a bar chart. The default is line. To do so, we will set kind to bar. I could also customize the X-axis label and the Y-axis label. Matplotlib's extensive customization capabilities are both a strength and a complexity. It offers numerous ways to modify your plots, which can sometimes feel overwhelming. However, by starting with basic plots, often from data found in data frames and gradually exploring more advanced features, you can harness its full potential to create clear and compelling visualizations within Excel.

Contents