{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting with plot.ly backend" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Basic imports\n", "from physt.examples import normal_h2, normal_h1\n", "from physt.plotting import plotly\n", "import physt.plotting\n", "\n", "import numpy as np\n", "np.random.seed(42)\n", "\n", "# Set that we want plotly\n", "physt.plotting.set_default_backend(\"plotly\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following lines are necessary if you want to display plots in the Jupyter notebook." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.offline as po\n", "po.init_notebook_mode()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Define the 1-D example\n", "H = normal_h1()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default plot is `bar`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "name": "normal", "opacity": 1, "type": "bar", "uid": "206477f2-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 0.784863795805467, 0.7848637958054665, 0.784863795805467, 0.784863795805467, 0.784863795805467, 0.7848637958054665, 0.7848637958054674, 0.7848637958054665, 0.7848637958054665, 0.7848637958054678 ], "x": [ -3.529968353715609, -2.745104557910142, -1.9602407621046753, -1.1753769662992084, -0.39051317049374146, 0.39435062531172527, 1.1792144211171922, 1.9640782169226592, 2.7489420127281257, 3.533805808533593 ], "y": [ 10, 88, 485, 1605, 2831, 2844, 1543, 498, 88, 8 ] } ], "layout": { "barmode": "overlay" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "H.plot() # Same as H.plot.bar()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "mode": "lines", "name": "normal", "type": "scatter", "uid": "207e7ddc-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ -3.529968353715609, -2.745104557910142, -1.9602407621046753, -1.1753769662992084, -0.39051317049374146, 0.39435062531172527, 1.1792144211171922, 1.9640782169226592, 2.7489420127281257, 3.533805808533593 ], "y": [ 10, 88, 485, 1605, 2831, 2844, 1543, 498, 88, 8 ] } ], "layout": {} }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "H.plot.line()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "mode": "markers", "name": "normal", "type": "scatter", "uid": "208e3254-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ -3.529968353715609, -2.745104557910142, -1.9602407621046753, -1.1753769662992084, -0.39051317049374146, 0.39435062531172527, 1.1792144211171922, 1.9640782169226592, 2.7489420127281257, 3.533805808533593 ], "y": [ 10, 88, 485, 1605, 2831, 2844, 1543, 498, 88, 8 ] } ], "layout": {} }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "H.plot.scatter()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib compatibility\n", "\n", "Plot.ly supports to convert matplotlib figures into plot.ly ones.\n", "You can use this compatibility with several fancy properties.\n", "\n", "Just add the `mpl` keyword argument to your plotting call:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "line": { "color": "rgba (255, 0, 0, 1)", "dash": "solid", "width": 5 }, "mode": "lines", "name": "normal", "type": "scatter", "uid": "212d2774-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ -3.529968353715609, -2.745104557910142, -1.9602407621046753, -1.1753769662992084, -0.39051317049374146, 0.39435062531172527, 1.1792144211171922, 1.9640782169226592, 2.7489420127281257, 3.533805808533593 ], "xaxis": "x", "y": [ 10, 88, 485, 1605, 2831, 2844, 1543, 498, 88, 8 ], "yaxis": "y" } ], "layout": { "autosize": false, "height": 345, "hovermode": "closest", "margin": { "b": 41, "l": 36, "pad": 0, "r": 14, "t": 26 }, "showlegend": false, "title": "1D normal distribution", "titlefont": { "color": "#000000", "size": 12 }, "width": 460, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "mirror": "ticks", "nticks": 9, "range": [ -3.9224002516183423, 3.9262377064363267 ], "showgrid": false, "showline": true, "side": "bottom", "tickfont": { "size": 10 }, "ticks": "inside", "title": "x", "titlefont": { "color": "#000000", "size": 10 }, "type": "linear", "zeroline": false }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "mirror": "ticks", "nticks": 7, "range": [ 0.8573324964312685, 3.536714962374179 ], "showgrid": false, "showline": true, "side": "left", "tickfont": { "size": 10 }, "ticks": "inside", "type": "log", "zeroline": false } } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "H.plot.line(color=\"red\", lw=5, yscale=\"log\", mpl=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot.ly object\n", "\n", "If you want to further manipulate the figures, you can return them from\n", "the function as-is using the `raw` keyword." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Figure({\n", " 'data': [{'mode': 'markers',\n", " 'name': 'normal',\n", " 'type': 'scatter',\n", " 'uid': '2138449c-3e92-11e9-ba8b-3b7509a6e7b7',\n", " 'x': array([-3.52996835, -2.74510456, -1.96024076, -1.17537697, -0.39051317,\n", " 0.39435063, 1.17921442, 1.96407822, 2.74894201, 3.53380581]),\n", " 'y': array([ 10., 88., 485., 1605., 2831., 2844., 1543., 498., 88., 8.])}],\n", " 'layout': {}\n", "})" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H.plot.scatter(raw=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2D histograms" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "H2 = normal_h2()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "type": "heatmap", "uid": "214b3bd8-3e92-11e9-ba8b-3b7509a6e7b7", "z": [ [ 0, 0, 0, 2, 6, 2, 2, 1, 0, 0 ], [ 0, 0, 9, 26, 45, 41, 14, 6, 2, 0 ], [ 0, 5, 41, 127, 194, 172, 91, 45, 4, 1 ], [ 5, 22, 126, 401, 558, 579, 327, 123, 18, 1 ], [ 6, 41, 192, 556, 879, 846, 479, 187, 29, 8 ], [ 2, 30, 146, 426, 672, 640, 403, 134, 27, 2 ], [ 1, 19, 57, 161, 240, 313, 204, 55, 9, 0 ], [ 1, 1, 15, 34, 52, 59, 35, 13, 3, 0 ], [ 0, 2, 0, 2, 7, 6, 5, 2, 1, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 ] ] } ], "layout": {} }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Default is heatmap\n", "H2.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Collections" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from physt.histogram_collection import HistogramCollection\n", "\n", "collection = HistogramCollection.h1({\n", " \"small\": np.random.normal(160, 20, 600),\n", " \"tall\": np.random.normal(180, 20, 1000),\n", " \"huge\": np.random.normal(200, 20, 400),\n", " \"gigantic\": np.random.normal(220, 20, 200)\n", "}, \"human\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "mode": "lines", "name": "small", "type": "scatter", "uid": "2166ad5a-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 1, 1, 3, 12, 20, 45, 84, 126, 121, 91, 61, 18, 9, 6, 2, 0, 0, 0, 0, 0 ] }, { "mode": "lines", "name": "tall", "type": "scatter", "uid": "2166b08e-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 2, 4, 14, 51, 93, 154, 197, 171, 148, 90, 48, 24, 4, 0, 0, 0, 0 ] }, { "mode": "lines", "name": "huge", "type": "scatter", "uid": "2166b138-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 1, 2, 8, 17, 31, 64, 76, 81, 64, 29, 16, 9, 2, 0, 0 ] }, { "mode": "lines", "name": "gigantic", "type": "scatter", "uid": "2166b1c4-3e92-11e9-ba8b-3b7509a6e7b7", "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 2, 7, 8, 29, 35, 33, 37, 21, 17, 6, 4, 1 ] } ], "layout": {} }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "collection.plot.line()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "name": "small", "opacity": 0.3, "type": "bar", "uid": "21890102-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 1, 1, 1, 0.8571428571428571, 0.8333333333333334, 0.75, 0.6131386861313869, 0.5550660792951542, 0.41156462585034015, 0.2791411042944785, 0.20065789473684212, 0.06642066420664207, 0.04186046511627907, 0.039735099337748346, 0.021739130434782608, 0, 0, 0, 0, 0 ] }, { "name": "tall", "opacity": 0.3, "type": "bar", "uid": "218911ce-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0.14285714285714285, 0.16666666666666666, 0.23333333333333334, 0.3722627737226277, 0.40969162995594716, 0.5238095238095238, 0.6042944785276073, 0.5625, 0.5461254612546126, 0.4186046511627907, 0.31788079470198677, 0.2608695652173913, 0.0975609756097561, 0, 0, 0, 0 ] }, { "name": "huge", "opacity": 0.3, "type": "bar", "uid": "218919d0-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 0.016666666666666666, 0.014598540145985401, 0.03524229074889868, 0.05782312925170068, 0.0950920245398773, 0.21052631578947367, 0.28044280442804426, 0.3767441860465116, 0.423841059602649, 0.31521739130434784, 0.3902439024390244, 0.34615384615384615, 0.25, 0, 0 ] }, { "name": "gigantic", "opacity": 0.3, "type": "bar", "uid": "21891f48-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0.006802721088435374, 0.02147239263803681, 0.02631578947368421, 0.1070110701107011, 0.16279069767441862, 0.2185430463576159, 0.40217391304347827, 0.5121951219512195, 0.6538461538461539, 0.75, 1, 1 ] } ], "layout": { "barmode": "overlay" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Let's see normalized histograms in the collection\n", "collection.normalize_bins().plot(barmode=\"overlay\", alpha=0.3)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "name": "small", "opacity": 1, "type": "bar", "uid": "21b07084-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 1, 1, 1, 0.8571428571428571, 0.8333333333333334, 0.75, 0.6131386861313869, 0.5550660792951542, 0.41156462585034015, 0.2791411042944785, 0.20065789473684212, 0.06642066420664207, 0.04186046511627907, 0.039735099337748346, 0.021739130434782608, 0, 0, 0, 0, 0 ] }, { "name": "tall", "opacity": 1, "type": "bar", "uid": "21b07ba6-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0.14285714285714285, 0.16666666666666666, 0.23333333333333334, 0.3722627737226277, 0.40969162995594716, 0.5238095238095238, 0.6042944785276073, 0.5625, 0.5461254612546126, 0.4186046511627907, 0.31788079470198677, 0.2608695652173913, 0.0975609756097561, 0, 0, 0, 0 ] }, { "name": "huge", "opacity": 1, "type": "bar", "uid": "21b07f20-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 0.016666666666666666, 0.014598540145985401, 0.03524229074889868, 0.05782312925170068, 0.0950920245398773, 0.21052631578947367, 0.28044280442804426, 0.3767441860465116, 0.423841059602649, 0.31521739130434784, 0.3902439024390244, 0.34615384615384615, 0.25, 0, 0 ] }, { "name": "gigantic", "opacity": 1, "type": "bar", "uid": "21b080c4-3e92-11e9-ba8b-3b7509a6e7b7", "width": [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ], "x": [ 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275 ], "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0.006802721088435374, 0.02147239263803681, 0.02631578947368421, 0.1070110701107011, 0.16279069767441862, 0.2185430463576159, 0.40217391304347827, 0.5121951219512195, 0.6538461538461539, 0.75, 1, 1 ] } ], "layout": { "barmode": "stack" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# ...and how they look like when stacked\n", "collection.normalize_bins().plot(barmode=\"stack\")" ] } ], "metadata": { "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }