Skip to main content

Day 31 - Visualizing Data with Python

Skills: None

Pre-reading: 10.1.7, 10.1.8

Intro (15 mins)

Today we learn Python data visualization, which is more complex than Pyret but offers more control and customization options.

Pyret visualization (as you saw in Day 12):

# Alternative: Load from CSV
# include csv
# orders = load-table: dish, quantity, total_price
# source: csv-table-url("https://pdi.run/f25-restaurant-orders.csv", default-options)
# end

# Create table manually
orders = table: dish :: String, quantity :: Number, total_price :: Number
row: "Pizza", 2, 25.0
row: "Salad", 1, 8.75
row: "Pizza", 1, 12.50
row: "Burger", 3, 30.0
row: "Salad", 2, 17.50
end

# Simple, built-in functions for common charts
freq-bar-chart(orders, "dish") # Frequency bar chart
histogram(orders, "total_price", 5) # Histogram
scatter-plot(orders, "quantity", "total_price") # Scatter plot

Python visualization:

import pandas as pd
import matplotlib.pyplot as plt

# Create the same table data as a DataFrame
orders = pd.DataFrame({
'dish': ['Pizza', 'Salad', 'Pizza', 'Burger', 'Salad'],
'quantity': [2, 1, 1, 3, 2],
'total_price': [25.0, 8.75, 12.50, 30.0, 17.50]
})

# Frequency bar chart (like Pyret's freq-bar-chart)
dish_counts = orders['dish'].value_counts()
plt.figure()
plt.bar(dish_counts.index, dish_counts.values)
plt.xlabel('Dish')
plt.ylabel('Count')
plt.title('Frequency of Dishes Ordered')
plt.show()

More complex example - scatter plot:

# Pyret way:
# scatter-plot(orders, "quantity", "total_price")

# Python way:
plt.figure()
plt.scatter(orders['quantity'], orders['total_price'])
plt.xlabel('Quantity')
plt.ylabel('Total Price ($)')
plt.title('Quantity vs Price')
plt.show()

Histogram comparison:

# Pyret way:
# histogram(orders, "total_price", 5)

# Python way:
plt.figure()
plt.hist(orders['total_price'], bins=5)
plt.xlabel('Total Price ($)')
plt.ylabel('Frequency')
plt.title('Distribution of Order Prices')
plt.show()

Class Exercises (40 mins)

Recreating Pyret charts in Python:

  • Create the workout DataFrame from Day 29. Make a frequency bar chart of workout activities (like Pyret's freq-bar-chart).
  • Using the same workout data, create a histogram of workout durations (like Pyret's histogram).
  • Make a scatter plot of duration vs calories_burned (like Pyret's scatter-plot).

Python-specific features:

  • Save one of your plots to a PNG file using plt.savefig('my_chart.png').

Comparing approaches:

  • Compare the number of lines of code needed: Pyret's freq-bar-chart(table, "column") vs the Python equivalent.
  • What are the advantages of Python's approach? What are the disadvantages?
  • Try creating the same chart in both Pyret and Python - which is easier? Which gives you more control?

Wrap-up (5 mins)

  • Python visualization requires more code than Pyret but offers much more customization and control.
  • Pyret's approach (freq-bar-chart, histogram, etc.) is simpler for basic charts, while Python's matplotlib gives you professional-level plotting capabilities.
  • Both approaches help you see patterns in data.