Histograms for ONGC - open, low, high and adjusted close

Published: 23 April 2024| Version 1 | DOI: 10.17632/nr7whvc92c.1
Contributor:
Sunil Maria Benedict

Description

The provided code performs data analysis and visualization on historical stock price data for the Oil and Natural Gas Corporation (ONGC) from a CSV file. The code utilizes the pandas library for data manipulation and matplotlib.pyplot for generating histograms to visualize the distribution of various price metrics. Loading Data: The code begins by importing the pandas library as pd and the matplotlib.pyplot library as plt. The file path of the CSV file containing the ONGC stock price data is specified. The data is loaded into a DataFrame named df using the read_csv() function from pandas. Extracting Required Columns: The code extracts the required columns from the DataFrame, including 'Open', 'High', 'Low', 'Close', and 'Adj Close' prices, and assigns them to separate variables. Summary Statistics: Summary statistics are computed for each extracted column using the describe() function, which provides metrics such as count, mean, standard deviation, minimum, 25th percentile, median, 75th percentile, and maximum values. Plotting Histograms: Histograms are created for each price metric ('Open', 'High', 'Low', 'Close', 'Adj Close') using the plt.hist() function. Each histogram is plotted in a separate subplot within a single figure using the plt.subplot() function. The number of bins for each histogram is set to 20 to visualize the distribution of prices across different ranges. Titles and labels are added to each subplot to provide context to the plotted data. Displaying Plots: The plt.tight_layout() function adjusts the spacing between subplots to prevent overlapping. Finally, the plt.show() function is called to display the histograms. Overall, the code efficiently analyzes the distribution of ONGC stock prices across different metrics and visualizes them using histograms, allowing for easy interpretation of the data's characteristics and patterns.

Files

Steps to reproduce

import pandas as pd import matplotlib.pyplot as plt # Load the data file_path = '/Users/sunilbenedict/Downloads/ONGC.NS.csv' df = pd.read_csv(file_path) # Extract the required columns open_prices = df['Open'] high_prices = df['High'] low_prices = df['Low'] close_prices = df['Close'] adj_close_prices = df['Adj Close'] # Summary statistics open_stats = open_prices.describe() high_stats = high_prices.describe() low_stats = low_prices.describe() close_stats = close_prices.describe() adj_close_stats = adj_close_prices.describe() # Print summary statistics print("Summary statistics for 'Open' column:") print(open_stats) print("\nSummary statistics for 'High' column:") print(high_stats) print("\nSummary statistics for 'Low' column:") print(low_stats) print("\nSummary statistics for 'Close' column:") print(close_stats) print("\nSummary statistics for 'Adj Close' column:") print(adj_close_stats) # Plot histograms for each field plt.figure(figsize=(10, 6)) # Histogram for 'Open' prices plt.subplot(2, 3, 1) plt.hist(open_prices, bins=20, color='skyblue', edgecolor='black') plt.title('Histogram of Open Prices') plt.xlabel('Open Price') # Histogram for 'High' prices plt.subplot(2, 3, 2) plt.hist(high_prices, bins=20, color='salmon', edgecolor='black') plt.title('Histogram of High Prices') plt.xlabel('High Price') # Histogram for 'Low' prices plt.subplot(2, 3, 3) plt.hist(low_prices, bins=20, color='green', edgecolor='black') plt.title('Histogram of Low Prices') plt.xlabel('Low Price') # Histogram for 'Close' prices plt.subplot(2, 3, 4) plt.hist(close_prices, bins=20, color='orange', edgecolor='black') plt.title('Histogram of Close Prices') plt.xlabel('Close Price') # Histogram for 'Adj Close' prices plt.subplot(2, 3, 5) plt.hist(adj_close_prices, bins=20, color='purple', edgecolor='black') plt.title('Histogram of Adj Close Prices') plt.xlabel('Adj Close Price') # Adjust layout and display the plots plt.tight_layout() plt.show()

Institutions

Independent

Categories

Historical Analysis, Stock Exchange, Stock Price, Price Volatility

Licence