Before we can start creating plots, we need to bring the necessary tools into our Python environment. Just like you might gather ingredients before cooking, we need to import
the libraries we plan to use. In Python, the import
statement makes the code from another module (or library) available in your current script or interactive session.
For data visualization, the two main libraries we've discussed are Matplotlib and Seaborn. There are established conventions for importing them, which makes Python code more readable and understandable for others (and for your future self!).
The primary interface for creating plots in Matplotlib is its pyplot
module. It contains functions that allow you to generate figures, create plotting areas, plot lines, add labels, and much more. The standard way to import pyplot
is to give it a shorter alias, plt
. This alias is widely used in the Python data science community.
import matplotlib.pyplot as plt
By using as plt
, we can now access pyplot
functions by typing plt.
followed by the function name (e.g., plt.plot()
), instead of the longer matplotlib.pyplot.plot()
. This saves typing and keeps the code cleaner.
Seaborn is typically imported with the alias sns
. While the origin of this specific alias isn't definitively known, it's the standard convention you'll see everywhere. Seaborn functions often work directly with Pandas DataFrames (which we'll cover later) and can automatically handle many plotting details.
import seaborn as sns
With this import, you can use Seaborn functions like sns.lineplot()
or sns.set_theme()
.
While Matplotlib and Seaborn are our focus for plotting, data often comes in formats managed by other libraries. NumPy is fundamental for numerical operations and often used for creating sample data, while Pandas is essential for data manipulation and analysis, particularly with its DataFrame structure. It's common practice to import these as well, especially when working with data files.
import numpy as np
import pandas as pd
Again, np
for NumPy and pd
for Pandas are the standard aliases.
In most data visualization scripts or notebooks, you'll start by importing these libraries at the very beginning:
# Core visualization libraries
import matplotlib.pyplot as plt
import seaborn as sns
# Data manipulation libraries (often used together)
import numpy as np
import pandas as pd
# (Optional) Set a Seaborn style for nicer plots - we'll discuss this later
sns.set_theme()
print("Libraries imported successfully!")
Running these import statements makes the functions and objects within matplotlib.pyplot
, seaborn
, numpy
, and pandas
available for use in the rest of your code, under their respective aliases (plt
, sns
, np
, pd
).
Now that we know how to bring our tools into the workspace, we are ready to create our first visualization in the next section.
© 2025 ApX Machine Learning