Data Visualization with Python


Data Visualization with Python

Corey
Today we're diving into data visualization with Python, specifically using the matplotlib library. But first, let's talk about why visualization matters. If you have a dataset with thousands of columns and millions of rows, finding patterns manually is nearly impossible. Visualizing the data turns those raw numbers into something you can actually see and interpret.

Anna
Exactly. For example, think about a dataset with records of prehistoric animals. If you just look at numbers, it's hard to compare size, speed, and aggressiveness. But if you visualize each animal as an image, you instantly see that a mammoth is huge, slow, and gentle, while a saber-tooth cat is smaller, fast, and aggressive. That's the power of visualization.

Corey
And it's not just about pretty pictures. There's a famous example called the Anscombe Quartet. It's four datasets that have nearly identical summary statistics, like the mean and standard deviation. But when you plot them, you see completely different patterns — linear, curved, outliers. That shows why you always have to visualize your data, not just rely on numbers.

Anna
Absolutely. So let's get into the tools. Python has a bunch of visualization libraries: matplotlib, seaborn, ggplot, plotly, and more. But in this session, we focus on matplotlib. Let's start with a basic line plot.

Corey
Right. You'd import matplotlib.pyplot as plt and maybe numpy for data. A simple line plot is just plt.plot(x, y). For instance, if x is [1,2,3,4,5] and y is [2,4,6,8,10], you get a straight line. But you can customize it — change the color with the 'color' parameter, add a title with plt.title(), and label axes with plt.xlabel() and plt.ylabel().

Anna
And subplots are great for comparing multiple plots in one figure. You use plt.subplot(rows, columns, index) to activate a subplot, then plot as usual. For example, if you want two plots side by side, you do subplot(1,2,1) for the first and subplot(1,2,2) for the second. Then plt.show() renders everything.

Corey
Then there are bar plots, which are for categorical data. You create a dictionary with fruit names and quantities, extract the keys and values, then do plt.bar(names, values). You can add the same customization — title, labels, color — and even create horizontal bar plots with plt.barh() instead of plt.bar().

Anna
Now, a common point of confusion: what's the difference between a bar plot and a histogram? A bar plot is for categorical variables — like types of fruit. A histogram is for numerical data — it shows the distribution of continuous values. For a histogram, you use plt.hist(data) and you can adjust the number of bins. For example, a histogram of sepal lengths from the iris dataset shows that most flowers have sepal lengths between 5 and 6.5, with very few above 7.

Corey
And you can customize the histogram too — change the color, or increase the number of bins to get a finer distribution. If you set bins to 50 or 100, you get a much smoother curve. But too many bins can be noisy, so you have to find the sweet spot.

Anna
Scatter plots show the relationship between two numerical variables. Use plt.scatter(x, y). You can change the size of markers with the 's' parameter, the shape with the 'marker' parameter, and even the overall figure size with plt.figure(figsize=(width, height)). For instance, marker='o' gives circles, marker='^' gives triangles.

Corey
Box plots are fantastic for showing the distribution of a dataset — median, quartiles, and outliers. You pass a list of lists to plt.boxplot(data). The line in the middle of the box is the median. For example, if you have three lists with different ranges, you can compare their medians and spreads visually.

Anna
And there's also the violin plot, which is like a box plot but shows the density of the data as a shape that looks like a violin. It's created with plt.violinplot(data). You can show the median with showmedian=True and the mean with showmeans=True. In some distributions, mean and median might be the same if the data is symmetric.

Corey
Pie charts represent proportions. With plt.pie(values, labels=labels), you get a circular chart. Add percentages with the autopct parameter, like '%1.1f%%'. You can explode a slice to highlight it using the explode parameter, and add a shadow with shadow=True. For example, if you have fruits and quantities, you can show that grapes make up over half the basket.

Anna
And you can create a donut plot by overlaying a smaller white pie chart on top. You'd make two pie charts: one for the outer ring with a larger radius, and one for the inner white circle with a smaller radius. The inner pie should have the same values as the outer? No, actually the inner is a separate white circle — you can set its values to a constant and color it white. That gives you the donut shape.

Corey
Exactly. And area plots, or stack plots, show how different components add up over a continuum. Use plt.stackplot(x, y1, y2, ...). They're like histograms but show cumulative area. For instance, with x from 10 to 90 and some y values, you can see the overall distribution of values.

Anna
Now, during the session there was a Q&A. Someone asked how to add another circle inside a donut plot. The answer is to add a third pie chart with an even smaller radius, say 0.5, and color it differently. That creates an inner ring, making three concentric rings.

Corey
Another question was about adding a line plot on top of an area plot. That's straightforward — just call plt.plot(x, y) after the stackplot, and you can set its color. They also asked about changing pie chart colors: use the 'colors' parameter with a list of color names that matches the number of slices.

Anna
And that wraps up the main content. The session also promoted a Python for Data Science course and a Data Science Architect course in association with IBM, which covers more advanced topics like machine learning, deep learning, and Tableau. But we covered the essentials of matplotlib: line, bar, scatter, histogram, box, violin, pie, donut, and area plots.

Corey
So to recap: always visualize to find patterns, use matplotlib for flexible plotting, and remember the key differences — bars for categories, histograms for numbers, scatter for relationships, box/violin for distributions, and pie for proportions. Happy plotting!

Ask anything
We've read this whole study set, so questions about it get the best answers.