Created
November 22, 2019 03:32
-
-
Save amitrani6/ea4cc87ef365fab72cd716acc5d5bbce to your computer and use it in GitHub Desktop.
Matplotlib OHLC Chart For The Campbell Soup Company
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code was adapted from Harrison Kinsley's tutorial | |
# Source: https://pythonprogramming.net/candlestick-ohlc-graph-matplotlib-tutorial/ | |
# Import the necessary libraries | |
import pandas as pd | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import matplotlib.dates as mdates | |
import matplotlib.ticker as mticker | |
# from matplotlib.finance import candlestick_ohlc | |
from mpl_finance import candlestick_ohlc | |
from matplotlib.pyplot import figure | |
%matplotlib inline | |
# Read in S&P 500 and Campbell Soup Company Data | |
cpb = pd.read_csv('supporting_files/CPB.csv') | |
# Make a copy of the data frame before converting the date | |
# column to Matplotlib's format | |
cpb_mpl = cpb.copy() | |
# Convert Date column to Matplotlib's float format | |
import matplotlib.dates as dates | |
cpb_mpl.Date = dates.datestr2num(cpb_mpl.Date) | |
# Create a list of lists where each inner-list represents | |
# one day's trading history | |
cpb_subset = cpb_mpl[['Date', 'Open', 'High', 'Low', 'Close', 'Volume']] | |
cpb_list = [list(x) for x in cpb_subset.values] | |
# Build the plot | |
fig = plt.figure(figsize=(16,8)) | |
ax1 = plt.subplot2grid((1,1), (0,0)) | |
candlestick_ohlc(ax1, cpb_list, width=0.4, colorup='#77d879', colordown='#db3f3f') | |
for label in ax1.xaxis.get_ticklabels(): | |
label.set_rotation(45) | |
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) | |
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) | |
ax1.grid(True) | |
plt.xlabel('Date') | |
plt.ylabel('Price') | |
plt.title('Campbell Soup Company') | |
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment