{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "flex_title = \"Plotly plots\"\n", "flex_orientation = \"rows\"\n", "flex_subtitle = \"built using jupyter-flex\"\n", "flex_source_link = \"https://github.com/danielfrg/jupyter-flex/blob/master/examples/plots/plotly.ipynb\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatter Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "import datetime\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import pandas_datareader.data as web\n", "\n", "import plotly.express as px\n", "import plotly.graph_objects as go\n", "\n", "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "margin = go.layout.Margin(l=20, r=20, b=20, t=30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "from scipy.stats import norm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "cls = np.repeat([\"A\", \"B\"], 10)\n", "x = np.arange(0, 20) + norm.rvs(20, 3, size=20)\n", "y = np.arange(0, 20) + norm.rvs(20, 3, size=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "df = pd.DataFrame({\"cls\": cls, \"x\": x, \"y\": y})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear Regression" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\", trendline=\"ols\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lowess Smoothed Fit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\", trendline=\"lowess\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### By class" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\", color=\"cls\", trendline=\"ols\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Density" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Gamma density" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "import scipy.special as sc\n", "import plotly.figure_factory as ff" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "df = pd.DataFrame({\n", " \"nu075\": np.random.gamma(1, 0.75, size=100),\n", " \"nu1\": np.random.gamma(1, 1, size=100),\n", " \"nu2\": np.random.gamma(1, 2, size=100),\n", "})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = ff.create_distplot([df[c] for c in df.columns], df.columns, show_hist=False)\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iris density" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "df = px.data.iris()\n", "fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\", marginal_y=\"rug\", marginal_x=\"histogram\")\n", "\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iris density countour" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "df = px.data.iris()\n", "fig = px.density_contour(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\", marginal_x=\"rug\", marginal_y=\"histogram\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "df = px.data.tips()\n", "fig = px.histogram(df, x=\"sex\", y=\"tip\", histfunc=\"avg\", color=\"smoker\", barmode=\"group\",\n", " facet_row=\"time\", facet_col=\"day\", category_orders={\"day\": [\"Thur\", \"Fri\", \"Sat\", \"Sun\"],\n", " \"time\": [\"Lunch\", \"Dinner\"]})\n", "\n", "fig.update_layout(margin=margin)\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "orientation=columns" ] }, "source": [ "# Time Series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Apple" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "start = datetime.datetime(2015, 1, 1)\n", "end = datetime.datetime(2019, 12, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "df_1 = web.DataReader(\"AAPL\", \"yahoo\", start, end).reset_index()\n", "df_2 = web.DataReader(\"MSFT\", \"yahoo\", start, end).reset_index()\n", "df_3 = web.DataReader(\"AMZN\", \"yahoo\", start, end).reset_index()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "margin = go.layout.Margin(l=20, r=20, b=20, t=30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = go.Figure([go.Scatter(x=df_1['Date'], y=df_1['Close'])])\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Microsoft" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = go.Figure([go.Scatter(x=df_2['Date'], y=df_2['Close'])])\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Amazon" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = go.Figure([go.Scatter(x=df_3['Date'], y=df_3['Close'])])\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tabs" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "tabs" ] }, "source": [ "## Tabs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tab 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "cls = np.repeat([\"A\", \"B\"], 10)\n", "x = np.arange(0, 20) + norm.rvs(20, 3, size=20)\n", "y = np.arange(0, 20) + norm.rvs(20, 3, size=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "df = pd.DataFrame({\"cls\": cls, \"x\": x, \"y\": y})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tab 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\", trendline=\"ols\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tab 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "fig = px.scatter(df, x=\"x\", y=\"y\", trendline=\"lowess\")\n", "\n", "fig.update_layout(margin=margin)\n", "fig" ] } ], "metadata": { "celltoolbar": "Tags", "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }