{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "URL: https://docs.bokeh.org/en/2.4.1/docs/gallery/stocks.html\n", "\n", "Most examples work across multiple plotting backends, this example is also available for:\n", "\n", "* [Bokeh - stocks example](../bokeh/stocks_example.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import holoviews as hv\n", "from holoviews import opts\n", "\n", "hv.extension('matplotlib')\n", "hv.output(fig='svg', dpi=120)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Defining the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from holoviews.operation.timeseries import rolling\n", "from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT\n", "\n", "\n", "def get_curve(data, label=''):\n", " df = pd.DataFrame(data)\n", " df['date'] = df.date.astype('datetime64[ns]')\n", " return hv.Curve(df, ('date', 'Date'), ('adj_close', 'Price'), label=label)\n", "\n", "hv.Dimension.type_formatters[np.datetime64] = '%Y'\n", "\n", "aapl = get_curve(AAPL, label='AAPL')\n", "goog = get_curve(GOOG, label='GOOG')\n", "ibm = get_curve(IBM, label='IBM')\n", "msft = get_curve(MSFT, label='MSFT')\n", "\n", "avg_curve = rolling(aapl, rolling_window=30).relabel('Average')\n", "avg_scatter = hv.Scatter((np.array(AAPL['date'], dtype=np.datetime64), np.array(AAPL['adj_close'])), \n", " ('date', 'Date'), ('adj_close', 'Price'), label='close')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color_cycle = hv.Cycle(values=['#A6CEE3', '#B2DF8A','#33A02C', '#FB9A99'])\n", "plot_opts = opts.Overlay(aspect=1, fig_size=200, legend_position='top_left')\n", "curve_opts = opts.Curve(color=color_cycle)\n", "\n", "stocks = (aapl * goog * ibm * msft).opts(plot_opts, curve_opts)\n", "\n", "appl_stats = (avg_scatter * avg_curve).opts(\n", " opts.Scatter(alpha=0.2, s=4, color='darkgrey'),\n", " opts.Curve(color='navy'), plot_opts)\n", "\n", "stocks + appl_stats" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }