{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "input_collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import bqplot.pyplot as plt\n", "from bqplot import LogScale, LinearScale, OrdinalScale, ColorScale" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple marks in a single figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(title='Multiple Marks')\n", "\n", "# create multiple marks. they'll be added to the same figure\n", "bar_chart = plt.bar([1, 2, 3, 4, 5], [2, 9, 10, 40, 40])\n", "line_chart = plt.plot([1, 2, 3, 4, 5], [10, 5, 30, 60, 20], 'rs-')\n", "\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sample Histogram with mid-points set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(title='Histogram')\n", "hist = plt.hist(sample=np.random.randn(100), bins=10, colors=['orange'])\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_axis = plt.axes()['sample']\n", "\n", "# Set the tick values to be the mid points of the bins\n", "x_axis.tick_values = hist.midpoints" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Line Chart Log Scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')\n", "size = len(dates)\n", "prices = 100 + 5 * np.cumsum(np.random.randn(size))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure()\n", "plt.scales(scales={'y': LogScale()})\n", "axes_options = {'x': dict(label='Date', grid_lines='dashed', num_ticks=4),\n", " 'y': dict(label='Log Price', tick_format='0.1f')}\n", "logline = plt.plot(dates, prices, 'm', axes_options=axes_options)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting min and max along an axis for plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure()\n", "line = plt.plot(np.arange(100), np.random.randn(100).cumsum(), 'r')\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## changing the min/max od x scale\n", "line.scales['x'].min = -10\n", "line.scales['x'].max = 110" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Marks which do not affect the domain along an axis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure()\n", "x = np.arange(50)\n", "y = np.cumsum(np.random.randn(50) * 100.0)\n", "line1 = plt.plot(x, y, 'b')\n", "line2 = plt.plot(x, y * 2, 'r', preserve_domain={'y': True})\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "line2.preserve_domain={}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preserve domain for color scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(50)\n", "y = np.cumsum(np.random.randn(50) * 100.0)\n", "\n", "fig = plt.figure()\n", "plt.scales(scales={'color': ColorScale(colors=['red', 'white', 'green'], mid=0.0)})\n", "scat1 = plt.scatter(x, y, color=y)\n", "scat2 = plt.scatter(x, y * 2, color=y * 2, preserve_domain={'color': True}, marker='cross')\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reversing a scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(50)\n", "y = np.cumsum(np.random.randn(50) * 100.0)\n", "\n", "fig = plt.figure()\n", "plt.scales(scales={'x': LinearScale(reverse=True)})\n", "line = plt.plot(x, y, 'b')\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fixing the domain of an ordinal scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(10)\n", "y = np.cumsum(np.random.randn(3, 10), axis=1)\n", "\n", "fig = plt.figure()\n", "axes_options = {'x': dict(set_ticks=True, grid_lines='none'),\n", " 'y': dict(grid_lines='none')}\n", "plt.scales(scales={'x': OrdinalScale(domain=list(range(20)))})\n", "bar_chart = plt.bar(x, y=y, \n", " colors=['hotpink', 'orange', 'green'],\n", " labels=['One', 'Two', 'Three'],\n", " axes_options=axes_options,\n", " display_legend=True)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying clip to marks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(100)\n", "y1, y2 = np.random.randn(2, 100).cumsum(axis=1)\n", "\n", "fig = plt.figure()\n", "plt.scales(scales={'x': LinearScale(min=10, max=90)})\n", "line1 = plt.plot(x, y1, 'r', labels=['Clipped Line'], display_legend=True)\n", "line2 = plt.plot(x, y2, 'g', apply_clip=False, labels=['Non clipped line'], display_legend=True)\n", "fig" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }