<jupyter_start><jupyter_text>Exercise 4: Plotting stock price data in tabs We will revisit the basic widgets and build a simple plot that will display the first 25 data points for the selected stock. The displayed stock can be changed with a drop-down menu. The dataset of this exercise contains temporal stock price data.   This means we'll be looking at data over a range of time.   > The dataset for this exercise is quite big and has to be downloaded and added into the data folder.   The dataset is available in the data folder of the GitHub repository; here is the link to it: https://bit.ly/2UaLtSV  Loading our dataset<jupyter_code># importing the necessary dependencies
import pandas as pd
# make bokeh display figures inside the notebook
from bokeh.io import output_notebook

output_notebook()
# loading the Dataset with geoplotlib
dataset = pd.read_csv('../../Datasets/stock_prices.csv')
# looking at the dataset
dataset.head()<jupyter_output><empty_output><jupyter_text>Since the date column has no information about the hour, minute, and second, we want to avoid displaying them in the visualization later on and simply display the year, month, and day.   Therefore we'll create a new column that holds the formatted short version of the date value.<jupyter_code># mapping the date of each row to only the year-month-day format
from datetime import datetime

def shorten_time_stamp(timestamp):
    shortened = timestamp[0]
    
    if len(shortened) > 10:
        parsed_date=datetime.strptime(shortened, '%Y-%m-%d %H:%M:%S')
        shortened=datetime.strftime(parsed_date, '%Y-%m-%d')
    
    return shortened

dataset['short_date'] = dataset.apply(lambda x: shorten_time_stamp(x), axis=1)

dataset.head()<jupyter_output><empty_output><jupyter_text>**Note:**   The exectuion of the cell will take a moment since it's a fairly large dataset.   Please be patient.   The last, newly added, column now holds the timestamp without the hour, minute, and second information.   ---  Creating a basic plot and adding a widget In this task, we will create a basic visualization with the stock price dataset. This will be your first interactive visualization in which you can dynamically change the stock that is displayed in the graph.   We will get used to one of the above described interactive widgets, the dropdown menu. It will be the main point of interaction for our visualization.<jupyter_code># importing the necessary dependencies 
from ipywidgets import interact

from bokeh.models.widgets import Panel, Tabs
from bokeh.plotting import figure, show<jupyter_output><empty_output><jupyter_text>In order to structure our notebook better, we want to write a adaptable method that gets a subsection of stock data as argument and builds a two tab `Pane` object, that lets us switch between the two views in our visualization.   The first tab will contain a line plot of the given data, the second one a circle-based representation of the same data.   A legend will display the name of the currently viewed at stock.<jupyter_code># method to build the tab-based plot
def get_plot(stock):
    stock_name=stock['symbol'].unique()[0]

    line_plot=figure(title='Stock prices', 
                     x_axis_label='Date', x_range=stock['short_date'], 
                     y_axis_label='Price in $USD')
    line_plot.line(stock['short_date'], stock['high'], legend_label=stock_name)
    line_plot.xaxis.major_label_orientation = 1

    circle_plot=figure(title='Stock prices', 
                       x_axis_label='Date', x_range=stock['short_date'], 
                       y_axis_label='Price in $USD')
    circle_plot.circle(stock['short_date'], stock['high'], legend_label=stock_name)
    circle_plot.xaxis.major_label_orientation = 1
    
    line_tab=Panel(child=line_plot, title='Line')
    circle_tab=Panel(child=circle_plot, title='Circles')
    tabs = Tabs(tabs=[ line_tab, circle_tab ])

    return tabs<jupyter_output><empty_output><jupyter_text>Before we can build our interaction, we have to get a list of all the stock names that are present in the dataset.   Once we have done that, we can then use this list as an input for the interact element.   With each interaction of the dropdown, our displayed data will then be updated.   To keep it simple, we only want to display the first 25 entries of each stock in this task.   By default, the stock of Apple should be displayed, it's symbol in the dataset is 'AAPL'.<jupyter_code># extracing all the stock names
stock_names=dataset['symbol'].unique()
# creating the dropdown interaction and building the plot
# based on selection

@interact(Stock=stock_names)
def get_stock_for(Stock='AAPL'):
    stock = dataset[dataset['symbol'] == Stock][:25]
    show(get_plot(stock))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0733598826241878, 'fraction_numerical': 0.0031439949696080486, 'mean_word_length': 4.43507972665148, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '52459', 'license': 'MIT', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1281, 'n_words': 568, 'path': 'Chapter06/Exercise6.04/Exercise6.04.ipynb', 'repo_name': 'jgoerner/Data-Visualization-with-Python', 'size': '4771'}