Skip to main content

Day 31 - Visualizing Data with Python

Skills: None

Pre-reading: 10.1.7 (EXPAND), 10.1.8

Intro (10 mins)

  • Today we will learn how to visualize data in Python using Pandas and Matplotlib.
  • Visualizations help us see patterns and trends that are hard to spot in raw tables.
  • Example: Plotting daily total sales from a restaurant orders DataFrame.
    import pandas as pd
    import matplotlib.pyplot as plt

    # Assume orders DataFrame exists with 'date' and 'total_price' columns
    daily_sales = orders.groupby('date')['total_price'].sum().reset_index()
    plt.figure()
    plt.plot(daily_sales['date'], daily_sales['total_price'], marker='o')
    plt.xlabel('Date')
    plt.ylabel('Total Sales ($)')
    plt.title('Daily Total Sales')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()
  • You can also make bar charts:
    dish_sales = orders.groupby('dish')['total_price'].sum().reset_index()
    plt.figure()
    plt.bar(dish_sales['dish'], dish_sales['total_price'], color='skyblue')
    plt.xlabel('Dish')
    plt.ylabel('Total Sales ($)')
    plt.title('Sales by Dish')
    plt.show()
  • Sometimes, data is in "wide" format (many columns, one per category) but plotting is easier in "tall" format (one column for category, one for value). Pandas' melt function helps convert wide to tall:
    wide_sales = pd.DataFrame({
    'week': ['Week1', 'Week2', 'Week3'],
    'north': [150, 200, 180],
    'south': [130, 170, 160]
    })
    tall_sales = wide_sales.melt(id_vars=['week'], var_name='region', value_name='sales')
    print(tall_sales)

Class Exercises (35 mins)

  • Using your restaurant orders DataFrame, plot total sales per day as a line plot.
  • Make a bar chart showing total sales for each dish.
  • Change the color or marker style in one of your plots. What effect does it have?
  • Plot the number of orders per order type (e.g., dine-in vs takeout) as a bar chart.
  • Create a DataFrame in wide format (e.g., sales by region for several weeks). Use melt to convert it to tall format, and plot sales by region for each week.
  • Try plotting a histogram of the total_price column.
  • What happens if you try to plot a column that doesn't exist? Try it and note the error.
  • Save one of your plots to a PNG file using plt.savefig('filename.png').
  • Add axis labels and a title to each of your plots.

Wrap-up (5 mins)

  • Visualizations help you see trends and communicate insights from your data.
  • You can use Pandas and Matplotlib to create many types of plots.