Adventures in Machine Learning

Mastering Data Visualization with Matplotlib and Pandas

When it comes to data visualization, Matplotlib is the go-to library for most Python programmers. It is a powerful tool that can create simple line charts to complex 3D visualizations.

However, learning Matplotlib can be a bit challenging for new users, especially when it comes to navigating its extensive object hierarchy and outdated syntax. In this article, we will explore different aspects of Matplotlib and delve into how it works.

We will take a closer look at the different challenges user face, including the confusing nature of the library, its extensive object hierarchy, and the differences between its stateful and stateless approaches. Understanding Matplotlib:

Challenges in learning Matplotlib

One of the primary keywords associated with Matplotlib is “confusing.” The library’s syntax is complex, often making it difficult for programmers to create visualizations efficiently. In addition, Matplotlib is a relatively old library, and some of its aspects are outdated, making it even more challenging to master.

Pylab and pyplot

Pylab and pyplot are two essential aspects of Matplotlib that make the library more user-friendly, especially for those who come from a MATLAB background. Pylab can be thought of as a MATLAB-like interface, while pyplot is a module that provides the same interface but with a more global style.

Users can import pyplot to create new figure windows and plot objects, customize the axes and tick marks, and display the plots.

Matplotlib Object Hierarchy

To understand Matplotlib’s object hierarchy is to understand its underlying structure. Matplotlib objects are organized into a tree structure that includes the Figure, the Axes, and the tick marks.

The Figure is the whole plot, while the Axes represent a single plot. Users can customize the Axes and the tick marks to create custom visualizations.

Stateful versus Stateless Approaches

Matplotlib has two approaches to creating visualizations: stateful and stateless. Stateful is a state-based or state-machine approach where Matplotlib keeps track of everything that the programmer wants to plot.

The object-oriented or OO approach is a stateless one, where programmers explicitly create everything they wish to plot using top-level functions in the Matplotlib API. The former is more intuitive, while the latter is more flexible and more efficient.

plt.subplots() Notation

Creating Figure with One Axes

To create a new figure window and one Axes object, users can utilize the subplots() method. The subplots() method generates the Figure object, which includes one Axes object in a one-row and one-column layout by default.

Users can then plot data and subsequently modify the layout.

Manipulating Axes Objects

After creating an Axes object, users can modify it using its instance methods. For instance, to create a stacked area chart, users can use the fill_between() method with the stack parameter set to true.

To add a title or a y-axis label, users can use the set_title() and set_ylabel() methods, respectively. To create a legend, one can generate one while plotting data.

Creating Figure with Multiple Subplots

For generating Figure objects with multiple subplots, users can use the subplots() method and specify the number of rows and columns they need. The subplots() method generates a NumPy array of subplots, which users can customize using iterable unpacking.

Users can then plot different types of data in each subplot using scatter() for scatter plot data and hist() for histograms. Users can also apply TeX markup to their visualizations to add mathematical notations.


In conclusion, Matplotlib is a powerful library for visualizing data in Python. However, it can be a bit challenging to master due to its confusing syntax, extensive object hierarchy, and outdated syntax.

By understanding Matplotlib’s different aspects, users can create better visualizations more efficiently, and make more informed decisions in their data analysis. Visualizing arrays with Matplotlib:

Matplotlib is a popular data visualization library that can handle simple line charts to complex 3D plots.

In this article, we will explore different techniques for visualizing arrays with Matplotlib, including using Matplotlib’s gridspec module, creating helper functions for in-plot titles and the different ways one can plot in Pandas. Working with gridspec module:

Matplotlib’s gridspec module aids users in creating different and complex layouts for their subplots.

To use gridspec, users can import the module and utilize the subplot2grid() method. The subplot2grid() method is a helper function that generates a subplot in a user-specified location within a grid.

By specifying the row and column where the subplot should reside, as well as the number of rows and columns that the subplot should span, users can customize their subplots quickly and effectively. Additional customization options include various parameters that can be passed to the subplot2grid() method.

These parameters allow users to specify the width ratios of columns and height ratios of rows, adjust the padding between subplots, and control the placement of each subplot. Helper function for in-plot title:

Creating a helper function for in-plot titles is useful for customizing the title font, size, and position, among other things.

To create a helper function, users can define a function that takes the axes object as a parameter. The function can then be used to add a title to the plot by utilizing the text box feature, which allows users to adjust the title position freely.

The text box feature can also be used to add annotations to the visualization. Users can specify the position of the annotation, the color of the text, and the size of the text, among other things.

Example: California housing data:

An example of how to utilize gridspec techniques and helper functions in Matplotlib would be to explore the California housing dataset. The California housing dataset contains the macroeconomic data of different states, along with the median house values for each state.

By visualizing this data, users can obtain insights into the real estate market and determine which states offer the best investment opportunities. To plot the California housing dataset, users can choose a grid layout that is most suitable for their visualization goals.

One example layout that can be utilized is a 3 by 2 grid, where the top row contains histograms of median house values for states with a higher than average median house value. The two columns of the second row should contain scatter plots of median house values vs.

median gross rent and median house values vs. population.

Plotting in Pandas:

Pandas is a popular data analysis library based on the NumPy library, and it comes with integrated data visualization capabilities. Pandas makes it easy for users to create basic plots and customize them to their liking using various parameters.

Creating basic plots:

To create a basic plot in Pandas, users can call the plot() method on a DataFrame or a Series. The plot() method generates a line chart by default, but users can specify other plot types such as histograms, scatter plots, or bar charts by using the kind parameter.

Customizing plots with parameters:

Pandas provides a variety of parameters that users can pass in when creating a plot. For instance, users can specify the title, xlabel, ylabel, color, kind, and legend parameters, among other things, in their Pandas plots.

Users can also customize their plots further by passing an Axes object into the plot() function, allowing for finer-grained customization of their visualizations. Visualizing data in DataFrames:

DataFrames are a powerful part of the Pandas library that makes it easy for users to visualize data contained in the data structure.

Users can plot data in a DataFrame by calling the plot() method and specifying the column that they wish to plot. Pandas allows users to plot line charts, bar charts, histograms, and scatter plots, among other things, directly from a dataframe.

This convenience enables users to produce visualizations with only a few lines of code.


In conclusion, Matplotlib and Pandas are two powerful libraries for visualizing data in Python. Matplotlib provides granular control over plot elements such as the figure, axes, and subplots.

Its gridspec module allows for the creation of complex, customized layouts. Pandas, on the other hand, offers a built-in method of visualizing data contained in data frames, enabling users to create different types of plots with ease.

By utilizing different techniques and parameters, users can generate custom visualizations, providing meaningful insights into their data. Wrapping up:

In this article, we explored different techniques for visualizing data using Matplotlib and Pandas.

Matplotlib is a powerful library that offers granular control over plot elements such as the figure, axes, and subplots. We delved into Matplotlib’s gridspec module, the hierarchy of Matplotlib objects, and the different state-based and object-oriented approaches to generating visualizations.

Additionally, we explored how Pandas offers an easy-to-use interface for visualizing data contained in data frames. Recap of key concepts:

To recap, Matplotlib’s design can be confusing and challenging to master, but the library provides an extensive object hierarchy that can be leveraged to create highly customized visualizations.

The gridspec module enables advanced layouts and nested subplots. Matplotlib’s stateful and stateless approaches provide various levels of control over the generated plots.

Pandas, on the other hand, offers an integrated method for visualizing data contained in data frames using a few lines of code.

Conclusion and next steps:

If you are new to Matplotlib, we hope this article gave you a better understanding of how it works and how to create custom visualizations using the library. Visualizing data is crucial to effective data analysis, and Matplotlib is an essential tool in any data scientist’s toolbox.

If you are interested in further exploring the capabilities of Matplotlib, there are plenty of online resources, tutorials, and examples available. The Matplotlib documentation is comprehensive and offers a user guide, gallery, and tutorials for further exploration.

Additionally, there are plenty of online tutorials and resources that can help you learn different Matplotlib techniques. More resources:

Here are some additional resources for learning Matplotlib:

– Matplotlib documentation:

– Real Python Matplotlib tutorials:

– DataCamp Matplotlib tutorials:

– Pyplot Tutorial – From Basic to Advanced:

Appendix A: Configuration and Styling

Customizing Matplotlib Configuration:

Matplotlib comes with built-in configurations that can be customized to suit specific plot designs.

Users can edit the configuration files directly or use the rcParams dictionary to change settings during runtime. The path to the configuration file can be found in the matplotlib.get_configdir() function.

A user can then locate the matplotlibrc file and edit the settings to their desired specifications.

Some of the customizable settings in Matplotlib include changing the default color map, specifying the default figure size, adjusting default font sizes, and setting default line widths.

By changing the settings, users can customize their plots to suit their specific needs. Additionally, users can create their custom configuration files, which override default settings, or they can use 3rd party configuration files such as seaborn, which is a popular library that provides different themes and styles to Matplotlib plots.

Styling plots with Matplotlib:

To apply a particular style to a Matplotlib plot, users can use style sheets. Matplotlib comes with several built-in stylesheets, including ‘seaborn’, ‘ggplot’, and ‘dark_background,’ among others.

Users can set the desired stylesheet using the set_style() method, after importing the style module using import In addition to the built-in stylesheets, users can create their custom stylesheets, allowing them to set specific settings such as colors, fonts, and line widths.

Matplotlib stylesheets can be used to set global settings with style.use(), modify specific settings with style.context(args…), or customize the visualization of a single plot by utilizing axes_style(), the which provides control over the grid, axis color, and zero line among other features. Appendix B: Interactive Mode

Overview of Matplotlib’s Interactive Mode:

Matplotlib’s interactive mode allows users to create plot animations dynamically.

Interactive mode can be enabled by calling plt.ion(), which enables a non-blocking interface with the backend GUI toolkit. Interactive mode can also be turned off by calling plt.ioff().

In interactive mode, users can modify elements of an existing plot and see how the plot changes in real-time. Interactive mode enables users to save animations by adding plotting calls to the animation object and using the save() method of the animation object.

To use the interactive mode, GUI backends must be installed. GUI backends provide a graphical user interface to create interactive plots.

Popular GUI toolkits used with Matplotlib include QT, GTK, and WXPython.


In conclusion, customizing and styling Matplotlib plots can greatly enhance data visualization. Matplotlib’s extensive configuration options and stylesheets provide various ways to customize plots based on a user’s specific needs.

Interactive mode, on the other hand, provides a dynamic interface for visualizing changes to plots in real-time. By using innovations like GUI backends, users can improve data analysis efficiency.

Matplotlib continues to be a popular visualization library among the Python community, and understanding the library’s configuration and interactive capabilities can take data analysis to the next level. In this article, we explored different techniques for visualizing data using Matplotlib and Pandas.

We discovered that Matplotlib’s design can be challenging, but the library provides extensive object hierarchy that can be leveraged to create custom visualizations. Pandas, on the other hand, offers an easy-to-use interface for visualizing data contained in data frames.

We have learned customization techniques using configurations and stylesheets and interactive mode to improve data analysis efficiency and create dynamic plots in real-time. Visualizing data is a crucial component of data analysis, and Matplotlib and Pandas are essential tools in any data scientist’s toolbox.

Popular Posts