Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_12/python-notebooks-data-wrangling/Visualization--All-About-Labeling.ipynb
1904 views
Kernel: Python 3

All about labels

The dataset in this example comes from Yahoo Finance: the stock price of Facebook and Twitter, from their IPO to May 1, 2016.

%matplotlib inline from pathlib import Path import matplotlib.pyplot as plt import pandas as pd DATA_DIR = Path('data', 'financial', 'raw')
twtr_df = pd.read_csv(DATA_DIR.joinpath('TWTR.csv'), parse_dates=['Date']) fb_df = pd.read_csv(DATA_DIR.joinpath('FB.csv'), parse_dates=['Date'])
twtr_df.head()
fb_df.tail()
# Basic chart fig, ax = plt.subplots() ax.plot(fb_df['Date'], fb_df['Adj Close'], color='#5566DD') ax.plot(twtr_df['Date'], twtr_df['Adj Close'], color='#88CCEE');
Image in a Jupyter notebook
from datetime import datetime fig, ax = plt.subplots() ax.plot(fb_df['Date'], fb_df['Adj Close'], color='#5566DD') ax.plot(twtr_df['Date'], twtr_df['Adj Close'], color='#88CCEE'); ax.set_xticks([datetime(2012, 1, 1), datetime(2013, 1, 1), datetime(2014, 1, 1), datetime(2015, 1, 1), datetime(2016, 1, 1)]);
Image in a Jupyter notebook
# use yearlocator from matplotlib.dates import YearLocator fig, ax = plt.subplots() ax.plot(fb_df['Date'], fb_df['Adj Close'], color='#5566DD') ax.plot(twtr_df['Date'], twtr_df['Adj Close'], color='#88CCEE'); ax.xaxis.set_major_locator(YearLocator());
Image in a Jupyter notebook
fig, ax = plt.subplots() ax.plot(fb_df['Date'], fb_df['Adj Close'], color='#5566DD') ax.plot(twtr_df['Date'], twtr_df['Adj Close'], color='#88CCEE'); ax.xaxis.set_major_locator(YearLocator()); ax.set_yticks(range(0, 120, 30));
Image in a Jupyter notebook
# manually set a y-limit so that there is space between 120 and the top of the chart fig, ax = plt.subplots() ax.plot(fb_df['Date'], fb_df['Adj Close'], color='#5566DD') ax.plot(twtr_df['Date'], twtr_df['Adj Close'], color='#88CCEE'); ax.xaxis.set_major_locator(YearLocator()); ax.set_yticks(range(0, 130, 30)) ax.set_ylim(ymax=130);
Image in a Jupyter notebook
# TODO: Add quarterly earnings reports