Lesson 48 · Mastering Pandas
How to Create Line, Bar, Histogram, and Box Plots Using Python and Pandas
In this lesson, you are going to explore simple but powerful ways to visualize data using pandas. We will use the Tips dataset, which contains restaurant…
- CourseMastering Pandas
- Lesson48 of 44
- Video17 min
- FormatJupyter notebook · 13 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbVisualizing Data with Pandas: Line, Bar, Histogram, and Box Plots#
In this lesson, you are going to explore simple but powerful ways to visualize data using pandas.
We will use the Tips dataset, which contains restaurant bills and corresponding tips.
By the end, you will know how to quickly create line plots, bar plots, histograms, and box plots for your own data analysis.
import warnings
warnings.filterwarnings('ignore')
# Data setup (Restaurant Tips Dataset)
import seaborn as sns
import numpy as np
np.random.seed(42)
df = sns.load_dataset('tips')
print(df.shape)
print(df.head(3))
Quick Data Overview#
Let us examine what is in the dataset.
Notice the following columns:
total_bill: The total restaurant bill (including tip).tip: The tip amount given by the customer.sex: The gender of the bill payer.smoker: Whether the party included smokers.day: Day of the week.time: Lunch or Dinner.size: Number of people at the table.
# Checking for missing values
print(df.isnull().sum())
# Summary statistics
print(df.describe())
Your First Plot: Line Plot of Total Bill#
Line plots are great for displaying continuous data and trends over an index.
Let us try plotting restaurant bills in the order they were recorded.
df['total_bill'].plot(kind='line', title='Total Bill Amounts (by entry)')
# Line plot of tips
df['tip'].plot(kind='line', title='Tip Amounts (by entry)')
Bar Plots: Comparing Tip Amounts by Day#
Bar plots are ideal for comparing quantities across categories.
For example, let us compare the average tip amount by day of the week.
# Compute mean tip per day and plot as bar chart
df.groupby('day')['tip'].mean().plot(kind='bar', title='Average Tip by Day', ylabel='Average Tip ($)')
# Bar plot: average total bill by smoker status
df.groupby('smoker')['total_bill'].mean().plot(kind='bar', title='Average Total Bill by Smoker', ylabel='Average Bill ($)')
Histograms: Distributions at a Glance#
Histograms provide a quick summary of the distribution of a numerical column.
Let us look at how total bills are distributed.
# Histogram of total_bill
df['total_bill'].plot(kind='hist', bins=20, title='Histogram of Total Bill', xlabel='Total Bill ($)')
# Histogram of tips
df['tip'].plot(kind='hist', bins=15, title='Histogram of Tip Amounts', xlabel='Tip Amount ($)')
Box Plots: Summarizing Data and Outliers#
A box plot summarizes key percentiles and highlights outliers.
Let us draw a box plot to compare total bills by time of day (Dinner vs. Lunch).
df.boxplot(column='total_bill', by='time', grid=False)
# Box plot: tip amount by party size
df.boxplot(column='tip', by='size', grid=False)
Bringing It Together: Multiple Plots in One Figure#
You can show several charts together to compare trends or patterns.
Let us use pandas to create a two-by-two grid of line and bar plots.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
df['total_bill'].plot(kind='line', ax=axs[0,0], title='Total Bill (Line)')
df['tip'].plot(kind='line', ax=axs[0,1], title='Tip (Line)', color='darkgreen')
df.groupby('day')['tip'].mean().plot(kind='bar', ax=axs[1,0], title='Average Tip by Day')
df['total_bill'].plot(kind='hist', bins=15, ax=axs[1,1], title='Total Bill Histogram', color='orange')
plt.tight_layout()
Challenge: Try Your Own Plots#
Create a histogram for the size column. Or, make a box plot showing tips by gender.
Remember: Use .plot(kind=...) with different kind values.
Pause and experiment for a few minutes.
# Extra: Save a plot as an image file
ax = df['total_bill'].plot(kind='hist', bins=15, title='Total Bill Histogram')
fig = ax.get_figure()
fig.savefig('total_bill_histogram.png')
Recap: What You Learned#
You created line, bar, histogram, and box plots using pandas.
You learned how to quickly explore patterns and outliers in real data.
Try using these plot types any time you work with your own datasets!
Happy coding!
Next Steps: Join the Community#
If you liked this lesson, subscribe for more practical data science.
Comment which other plots or datasets you want to see next!
Keep exploring and visualizing new ideas.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



