Automating Plots in Callisto Jupyter Notebooks
Welcome back to Exploring Inflation in the Car Market with Callisto. Now that we have plotted our graphs, this video is short because we are setting up the plan to figure out what’s causing the price increases.
Here’s what we know so far, the used car price index has spiked since 2020, unlike any other period in the 21st century.
Why might that be? Usually, if a price changes drastically for an item, there’s a good chance that there has been a change in the supply relative to the demand. The next simplest data we can look at is the Auto Sales (Demand) and Auto Inventory (Supply).
Furthermore, we can look at the price index for the manufacturers. This is the Producer Price Index – the prices for goods and services a producer pays to assemble a vehicle. To delve even deeper into car production, we can look at the price of raw materials. Cars are mostly made out of iron and aluminum. If the prices for those have risen, it will impact the final cost of the car.
If we plotted all these, it would take a lot of tedious coding. What can we do about that? Automation. We can put the basic tasks of searching, downloading, and plotting into a reusable function. Then we can use that for the data we want to see. To follow along with the code, make sure you have charted out the graph of the Used Car Index Prices in the 21st Century. By doing so, you would have done all the necessary steps that we want to include in our reusable graphing function.
To break it down even further with details, here’s our general goal: given an ID for a data series, generate the graph from the 21st Century portion of the data. Here are our steps:
- Download data from FRED for a given ID
- Extract the data for the 21st century
- Plot and annotate the data
Here’s how to do it in Callisto with Python, the `chart()` function:
def chart(id, search_data_frame): # Download Data for an ID from search result series = fred.get_series_latest_release(id) search_row = search_data_frame.loc[id] # set titles and units title = search_row["title"] units = search_row["units"] series.rename_axis(["Date"], inplace=True) series.rename(units, inplace=True) # only include 21st century dates series_21st = series[series.index.slice_indexer("2000-01-01")] # Plot data # Assign title, axis labels, and chart size data_plot = sns.relplot(data=series_21st, kind= "line") data_plot.fig.set_size_inches(13, 5) data_plot.fig.legend(labels=[title], fontsize=15, loc=1, bbox_to_anchor = (0.91, 1.1)) #return downloaded data series return series_21st # confirm that it works cpi_used_cars = chart(cpi_used_car_id, auto_search)
Run the above code in a new cell to try it out. It should show the Used Car Index prices for the 21st century.
Organizing our notebook!
Check out the next part in the series to learn how you can organize your notebook cells in Callisto. It’s always a good idea to keep everything neat and add descriptive comments for your code. Once that is done, we’ll get to use our awesome charting function!