
PYTHON — Matplotlib and Pandas in Python
It’s not that we use technology, we live technology. — Godfrey Reggio
Insights in this article were refined using prompt engineering methods.

PYTHON — History of Python Packaging
# Using Matplotlib and Pandas for Python Data Visualization
In this tutorial, we will explore how to use Matplotlib and Pandas for data visualization in Python. Matplotlib provides the functionality to visualize Python histograms with a versatile wrapper around NumPy’s histogram() function. We will also see how Pandas' Series.histogram() and DataFrame.histogram() use matplotlib.pyplot.hist() to draw histograms.
Visualizing Histograms with Matplotlib
Let’s start with an example of using Matplotlib to create a histogram. The following code snippet demonstrates how to create a histogram using Matplotlib:
import matplotlib.pyplot as plt
import numpy as np
# Generate Laplace-distributed data
np.random.seed(444)
np.set_printoptions(precision=3)
d = np.random.laplace(loc=15, scale=3, size=500)
# Plot the histogram
n, bins, patches = plt.hist(x=d, bins='auto', color='#0504aa', alpha=0.7, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('My Very Own Histogram')
plt.text(23, 45, r'$\mu=15, b=3$')
maxfreq = n.max()
plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
plt.show()In this example, we use matplotlib.pyplot.hist() to create a histogram of the Laplace-distributed data. The resulting histogram visualizes the frequency of values in the dataset.
Visualizing Histograms with Pandas
Now, let’s explore how to use Pandas to create a histogram. The following code snippet demonstrates how to create a histogram using Pandas:
import pandas as pd
import matplotlib.pyplot as plt
# Generate data on commute times
size, scale = 1000, 10
commutes = pd.Series(np.random.gamma(scale, size=size) ** 1.5)
# Plot the histogram using Pandas
commutes.plot.hist(grid=True, bins=20, rwidth=0.9, color='#607c8e')
plt.title('Commute Times for 1,000 Commuters')
plt.xlabel('Counts')
plt.ylabel('Commute Time')
plt.grid(axis='y', alpha=0.75)
plt.show()In this example, we use Pandas’ plot.hist() method to create a histogram of commute times for 1,000 commuters. This method simplifies the process of creating a histogram from a Pandas Series.
Conclusion
In this tutorial, we’ve seen how to visualize histograms using Matplotlib and Pandas in Python. Matplotlib provides a versatile way to create histograms, and Pandas simplifies the process of plotting histograms from Series and DataFrame data. These tools are valuable for visualizing and understanding the distribution of data in Python.

