Basic Plotting
Basic Plotting
Plotting for NumPy and pandas is commonly done using the matplotlib library,
particularly its sub-module pyplot, which provides a MATLAB-like interface for creating
static, animated, and interactive visualizations.
Importing pyplot
python
CopyEdit
import matplotlib.pyplot as plt
# Plot
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("x-axis")
plt.ylabel("y = sin(x)")
plt.grid(True)
plt.show()
# Create DataFrame
data = {
'Year': [2020, 2021, 2022, 2023],
'Sales': [150, 200, 250, 300]
}
df = pd.DataFrame(data)
# Plot
df.plot(x='Year', y='Sales', kind='line', marker='o')
plt.title("Annual Sales")
plt.ylabel("Sales")
plt.grid(True)
1
plt.show()
✅ Notes:
The example provided walks through basic plotting with Matplotlib's pyplot, particularly
in the context of analyzing alcohol consumption data across U.S. states over time. Here’s a
breakdown and explanation of what the code is doing in a more structured way:
1. Data Preparation
python
Imports essential libraries: matplotlib for plotting, pandas for data handling,
pickle for loading pre-saved data.
python
python
Retrieves beverage types and years by unstacking the DataFrame. The result is a
multi-indexed DataFrame where columns = beverage types, and years = years.
python
Loads state abbreviation info and sets state names as the index for easier merging.
python
2
frames = [pd.merge(alco[column].unstack(), states,
left_index=True, right_index=True).sort_values(2009)
for column in columns]
python
2. Plotting
python
matplotlib.style.use("ggplot")
python
STEP = 5
python
plt.subplot(2, 2, pos + 1)
draw(frame[frame.columns[:span]], cmap=style, aspect="auto")
plt.colorbar()
plt.title(column)
plt.xlabel("Year")
plt.xticks(range(0, span, STEP), frame.columns[:span:STEP])
plt.yticks(range(0, frame.shape[0], STEP), frame.Postal[::STEP])
plt.xticks(rotation=-17)
Key Concepts:
plt.subplot(2, 2, pos + 1): Arranges subplots in a 2x2 grid. pos + 1 picks the
position for each plot.
Plot types:
o contourf: filled contours
o contour: contour lines
3
o imshow: image plot (like a heatmap)
cmap: Defines the color map (visual palette).
aspect="auto": Ensures plots scale properly.
xticks and yticks: Adjusts tick marks and labels on x and y axes.
colorbar(): Adds a legend for the colors used.
Rotation: Tilts x-axis labels for better readability.
3. Saving or Displaying
python
plt.tight_layout()
plt.savefig("../images/pyplot-all.pdf")
# plt.show()
📝 Summary
This example:
Visualizes three types of alcohol consumption (Beer, Wine, Spirits) across states and
time.
Demonstrates merging datasets, styling subplots, using different plot types, and
exporting figures.
Illustrates good practices like colorbars, labeling, and layout adjustment.
4
Plot Types:
the various plot types supported by Matplotlib's pyplot module. Here's a clearer and more
structured summary of the most commonly used pyplot plotting functions and their
purposes:
5
Once you've chosen a plot type, the next step is to embellish your plot — to make it clear,
attractive, and informative. Embellishments help you tell the story behind the data.
python
plt.xscale("log")
plt.yscale("log")
python
plt.xlim(1975, 2010)
plt.ylim(1.0, 2.5)
Use themes/styles:
python
Use comic-style:
python
python
import matplotlib
matplotlib.rc("font", family="Arial")
6
Mark key events or peaks:
python
plt.annotate("Peak",
xy=(year, value),
xytext=(year+0.5, value+0.1),
arrowprops=dict(facecolor='black', shrink=0.2))
python
python
# Setup
BEVERAGE = "Beer"
years = alco.index.levels[1]
states = ("New Hampshire", "Colorado", "Utah")
7
# Add annotation for peak
peak_year = ydata.idxmax()
peak_value = ydata.max()
plt.annotate("Peak",
xy=(peak_year, peak_value),
xytext=(peak_year + 0.5, peak_value + 0.1),
arrowprops={"facecolor": "black", "shrink": 0.2})
# Add embellishments
plt.ylabel(BEVERAGE + " consumption")
plt.title("And now in xkcd...")
plt.legend()
plt.savefig("pyplot-legend-xkcd.pdf")
plt.show()
Note on
alco.ix[state]