Anyone interested in using Python plots and conducting data analysis with Python should consider a Matplotlib tutorial. Supplement the knowledge you gain from a data analyst class in San Francisco with the following information about Matplotlib, including how to use it to make a Python plot.

## A Quick Intro to Matplotlib

Most Matplotlib tutorials will assume that you already know that the program is a Python plotting library with a great deal of usefulness, particularly in data science.

## Parts of a Matplotlib Plot

One of the first things you should learn about Matplotlib is the basic elements found in the plots that the program creates. Here’s a quick overview:

*Figure*

The term “figure” refers to the overall page or window that the Python plot appears on and includes other components like legends, color bars and subtitles. You can use Matplotlib to make several figures, all of which can be independent of one another.

*Axes*

Adding axes to your figures forms the area where the data gets plotted with your functions. Keep in mind, figures can have more than one set of axes.

The axes include both the x- and y-axis. They also feature ticks, with minor and major tick lines as well as tick labels. Other components include the legend, title and axis labels, but these can be skipped depending on your preferences. You should always consider the gridlines and axis scales.

*Spines*

Spines refer to the lines connecting axis tick marks; they show the boundaries of your data area. Think of them as a basic black square that will appear if you have already initialized your axes but have not plotted any data. Most Matplotlib tutorials will likely explain that it is common for the top and right spines to be invisible.

## Key Functions

As you use the program, you’ll find there is a range of functions to include in your code. Of these, the most important are the *plot()* function and the *show()* function.

## Matplotlib’s Relationship with Pyplot and Pylab

If you choose to take a data analyst class in San Francisco or elsewhere, it will likely mention pyplot and pylab and should provide a Matplotlib tutorial.

*Pyplot*

You might have noticed that codes frequently feature “matplotlib.pyplot.” This is because Pyplot is one of the modules within the Matplotlib package that gives users an interface for automatically and implicitly creating figures as well as axes to make the plot you need.

A more in-depth Matplotlib tutorial will likely point out that this feature makes pyplot useful in cases when you need to quickly plot some data but do not want to instantiate any axes or figures. Pyplot will keep the axes and figures in mind as you make any customizations and as defaults initialize.

*Pylab*

Pylab is another module that typically gets installed with the Matplotlib package. Using pylab will automatically bulk import the numpy library along with pyplot.

The numpy library was historically a recommendation in data analyst classes for users working with arrays. It was particularly suggested in situations when you want to access plotting features and engage in interactive mathematics while working with arrays. However, you’re less likely to find the numpy library in modern Matplotlib tutorials because experts no longer recommend using it, particularly with Jupyter notebooks and IPython kernels.

The alternative to using the numpy library with pylab and pyplot in Matplotlib is to use “%matplotlib” and combine it with the appropriate backend, such as “qt” or “inline.” “Inline” is usually the best option due to the fact that it ensures the plots get embedded in the notebook.

## How to Create a Basic Plot

The first thing you need to do when creating a plot is to make your figure.

To initialize a figure, include the code:

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

## Adding Subplots

Once your figure and axes are set up, you can go on to create a subplot. Subplots will make your use of Matplotlib slightly more complicated, but once you get a hang of them, they are as straightforward as other parts of the program.

The goal of using subplots is to create a regular grid that you can place the axes on, which is good news since subplot and axes typically refer to the same thing. They will, however, appear differently in the coding. The *add_subplots()* function adds subplots, while the *add_axes() *function adds axes.

In a subplot, you’ll see code like *add_subplots(111)* where the “111” is equivalent to “1,1,1.” In subplots, the arguments refer to the number of rows, the number of columns and your plot number.

*Subplots vs. Axes*

Even in a quick Matplotlib tutorial, there are some minor differences between subplots and axes that are worth noting. Essentially, the difference comes down to the mechanism that your code uses for adding the axes. *add_axes() *involves passing a list that includes the lower-left point, then the width then the height. As such, the axes are created with absolute coordinates. By contrast, *add_subplot()* does not position the axes, but instead lets you situate them based on a subplot grid.

Most of the time, you will want to create axes using *add_subplot() *and *add_axes()* if the positioning matters. In situations when you need to add multiple subplots, you can also use *subplots()*.

## Changing Figure Size

Changing the size of figures in your Python plot is a useful skill for situations when you do not want to use the defaults.

To adjust the figure size, go to the pyplot module and the *plt.figure()* function. Add the *figsize* argument, including a tuple that has the width and height of the figure in inches. An example would be:

You can also use the same *figsize* in the *plt.subplots()* function to create similar resizing.

## Plotting Routines to Know

No comprehensive Matplotlib tutorial is complete without exploring the plotting routines.

The bare basics that you need to know are *scatter()* and *plot()* to draw unconnected points that get colored, scaled or draw points connected by markers or lines. You should also be aware of the following functions:

*ax.bar()*– Vertical rectangles*ax.barh()*– Horizontal rectangles*ax.axhline()*– Horizontal line across the axes*ax.vline()*– Vertical line across the axes*ax.stackplot()*– Stack plot*ax.fill()*– Filled polygons*ax.fill_between()*– Filled between 0 and y-values*ax.arrow()*– Arrow*ax.quiver()*– 2-D field with arrows*ax.hist()*– Histogram*ax.streamplot()*– 2-D fields with vectors*ax.boxplot()*– Boxplot*ax.violinplot()*– Violinplot

You can also get creative when using these functions to get different results. For example, if you wanted a stacked line graph, you could use *fill_between()* several times with overlapping areas for a stacked illusion.

An advanced **data analyst class in San Francisco** may also cover these advanced routines in a Matplotlib tutorial:

*ax.pcolormesh() or ax.pcolor()*– Pseudocolor plot*ax.contour()*– Contour plot*ax.clabel()*– Labeled contour plot*ax.contourf()*– Filled contour plot

**Please note, these articles are for educational purposes and the topics covered may not be representative of the curriculum covered in our boot camp. ***Explore our curriculum **to see what you’ll learn in our program.

**Explore our curriculum**to see what you’ll learn in our program.

## Get Program Info

Ready to learn more about **Berkeley Data Analytics** **Boot Camp in San Francisco? **Contact an admissions advisor at (510) 306-1218.