4 Things to Know to Have a Better Understanding of Matplotlib

    Getting familiar with one of the original Python data visualization libraries

    Photo by Tolga Ulkan on Unsplash

    Matplotlib was one of the first tools I learned about in my data science journey. I was amazed by how it allows for customizing almost every little piece on a data visualization.

    On the other hand, its syntax seemed complicated and hard to understand. I knew I could do something but it took a while to figure out how to do it. I felt more comfortable working with Matplotlib, once I learned about the structure of it and how plots are created.

    In this article, we will learn four things that will help you get a better understanding of Matplotlib.

    Since Python is an object-oriented programming language, it’s not a surprise that everything we see on a Matplotlib plot is an object with a type.

    Figures and Axes are the main object types, which serve as the basis of a plot. They are also known as composite object types. It is important to note that Axes is not the plural form of Axis. Thus the Axes object does not have anything to do with x-axis or y-axis.

    Figure can be considered as the outermost container that holds everything together. All other objects stay alive in this container. A Figure can have one or more Axes objects. In fact, we need an Axes object to actually draw something.

    A Figure can be created using the figure method of the matplotlib.pyplot interface.

    import matplotlib.pyplot as pltplt.figure()
    # output

    We have created a Figure object with the default size. Since it does not have any Axes object, nothing is shown.

    Let’s also add an Axes to the Figure object and see how it looks. We will also use the figsize parameter to customize the size of the Figure object.

    fig = plt.figure(figsize=(8,5))
    ax1 = fig.add_axes([0.1, 0.1, 0.6, 0.3])
    (image by author)

    The parameters used in the add_axes method define the location and size of the Axes object.

    In the previous example, we used the add_axes method to add an Axes to a Figure object. You don’t have to do this every time you need to add an Axes. In fact, you will rarely see the add_axes method used.

    Things like creating a Figure object or adding an Axes can sometimes be explicit. Don’t let this confuse you. For instance, in the following example, we will create a histogram of the price column of a DataFrame.

    (image by author)

    We just used the hist function to create the above histogram. We did not have to explicitly create a Figure and an Axes because these are automatically done once a plotting function (e.g. hist, plot) is called.

    We can, of course, create a Figure first and then the plot. Let’s say we want to change the Figure size for this histogram:

    (image by author)

    Plots without titles and axis labels are of no use. We should make the plots as informative as possible without making them difficult to read. However, titles and labels are a must-have.

    In Matplotlib, different methods are used for adding titles and labels to the Figure and Axes objects.

    When working on a Figure object, we can use the title, xlabel, and ylabel methods.

    # create a Figure
    plt.hist(df["Price"])plt.title("Histogram of Price", fontsize=15)
    plt.xlabel("Value", fontsize=14)
    plt.ylabel("Frequency", fontsize=14)
    (image by author)

    When working on an Axes object, the corresponding methods are set_ti

    # create a Figure with an Axes
    fig, ax = plt.subplots(figsize=(8,4))
    ax.hist(df["Price"])ax.set_title("Histogram of Price", fontsize=15)
    ax.set_xlabel("Value", fontsize=14)
    ax.set_ylabel("Frequency", fontsize=14)
    (image by author)

    Although we used different methods for adding titles and labels, the outputs were the same. It’s because we worked on a Figure with one Axes. We can observe the difference on a Figure with multiple Axes objects. We will cover that in the next section.

    A Figure object can contain multiple Axes objects. We can arrange the layout on the Figure using the subplots function. For instance, the following code snippet creates a Figure with 2 Axes objects placed as rows.

    fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8,5))
    (image by author)

    It’s empty since we did not plot anything. Let’s plot histograms and also set the titles on both the Figure and Axes objects.

    fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8,5))# figure title
    fig.suptitle("Histogram of Price and SalesQty", fontsize=15)
    # plot on the first Axes
    ax1.set_title("Price", fontsize=14)
    # plot on the second Axes
    ax2.set_title("SalesQty", fontsize=14)
    # adjust the spacing between subplots
    (image by author)

    When working with a Figure with subplots, we use the suptitle method to add a title to the entire Figure (not the title method).

    Matplotlib is a low-level library compared to other popular alternatives such as Seaborn and Plotly. As a result of this, we should expect to write more code to create data visualizations, which comes with the advantage of having full control over what we plot.

    Knowing the structure of the plots and how Matplotlib handles these customizations is an essential step to make the most out of this amazing library.

    What we have covered in this article will help you get a better understanding of how things work with Matplotlib.

    You can become a Medium member to unlock full access to my writing, plus the rest of Medium. If you already are, don’t forget to subscribe if you’d like to get an email whenever I publish a new article.

    Thank you for reading. Please let me know if you have any feedback.

    4 Things to Know to Have a Better Understanding of Matplotlib Republished from Source via

    Recent Articles


    Related Stories

    Stay on op - Ge the daily news in your inbox