#! /usr/bin/env python3 # def price_plots ( ): #*****************************************************************************80 # ## price_plots() reads price data and plots 3 prices on one display. # # Discussion: # # The file price_data.txt contains average monthly prices for 11 # consumer products, between February 2008 and February 2018. # # There are 241 records, each containing 13 items: # month, year, 11 prices # # Columns 3, 10 and 13 contain the prices of bananas, gas, and milk. # We want a single plot that shows the variation in these three prices. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 26 August 2021 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np print ( '' ); print ( 'price_plots():' ) print ( ' Use several line plots to display variation in price of three' ) print ( ' quantities over time.' ) # # Read the data. # price = np.loadtxt ( 'price_data.txt' ) month_num = price.shape[0] month = np.arange ( month_num ) # # Plot the results # plt.plot ( month, price[:,2], linewidth = 3 ) plt.plot ( month, price[:,9], linewidth = 3 ) plt.plot ( month, price[:,12], linewidth = 3 ) plt.grid ( True ) plt.legend ( [ 'Bananas', 'Gas', 'Milk' ] ) plt.xlabel ( '<-- Month index -->' ) plt.ylabel ( '<-- Price ($) -->' ) plt.title ( 'Prices, Feb 2008-Feb 2018' ) # # Save a copy of the plot. # filename = 'price_plots.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Display the plot. # plt.show ( ) plt.close ( ) return if ( __name__ == '__main__' ): price_plots ( )