Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "*Installation*\n",
  8. "\n",
  9. "```\n",
  10. "conda install -c bokeh datashader\n",
  11. "```"
  12. ]
  13. },
  14. {
  15. "cell_type": "code",
  16. "execution_count": null,
  17. "metadata": {},
  18. "outputs": [],
  19. "source": [
  20. "import datashader as ds\n",
  21. "import datashader.transfer_functions as tf\n",
  22. "import pandas as pd\n",
  23. "import numpy as np"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": null,
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "x = np.random.randn(10000000)\n",
  33. "y = np.sin(5 * x) + np.cos(6 * x) + 0.1 * np.random.randn(len(x))\n",
  34. "z = x ** 2 + y ** 2\n",
  35. "df = pd.DataFrame({'x': x, 'y': y, 'z':z})"
  36. ]
  37. },
  38. {
  39. "cell_type": "code",
  40. "execution_count": null,
  41. "metadata": {},
  42. "outputs": [],
  43. "source": [
  44. "cvs = ds.Canvas(plot_width=400, plot_height=400)\n",
  45. "agg = cvs.points(df, 'x', 'y')#, ds.mean('z'))\n",
  46. "tf.shade(agg, cmap=['lightblue', 'darkblue'], how='log')"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": null,
  52. "metadata": {},
  53. "outputs": [],
  54. "source": [
  55. "from bokeh.models import BoxZoomTool\n",
  56. "from bokeh.plotting import figure, output_notebook, show\n",
  57. "\n",
  58. "output_notebook()\n",
  59. "\n",
  60. "x_range = (-5, 5)\n",
  61. "y_range = (-5, 5)\n",
  62. "\n",
  63. "plot_width = int(750)\n",
  64. "plot_height = int(plot_width//1.2)\n",
  65. "\n",
  66. "def base_plot(tools='pan,wheel_zoom,reset',plot_width=plot_width, plot_height=plot_height, **plot_args):\n",
  67. " p = figure(tools=tools, plot_width=plot_width, plot_height=plot_height,\n",
  68. " x_range=x_range, y_range=y_range, outline_line_color=None,\n",
  69. " min_border=0, min_border_left=0, min_border_right=0,\n",
  70. " min_border_top=0, min_border_bottom=0, **plot_args)\n",
  71. " \n",
  72. " p.axis.visible = False\n",
  73. " p.xgrid.grid_line_color = None\n",
  74. " p.ygrid.grid_line_color = None\n",
  75. " \n",
  76. " p.add_tools(BoxZoomTool(match_aspect=True))\n",
  77. " \n",
  78. " return p\n",
  79. " \n",
  80. "options = dict(line_color=None, fill_color='blue', size=5)"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": null,
  86. "metadata": {},
  87. "outputs": [],
  88. "source": [
  89. "samples = df.sample(n=1000)\n",
  90. "p = base_plot()\n",
  91. "p.circle(x=samples['x'], y=samples['y'], **options)\n",
  92. "show(p)"
  93. ]
  94. },
  95. {
  96. "cell_type": "code",
  97. "execution_count": null,
  98. "metadata": {
  99. "collapsed": true
  100. },
  101. "outputs": [],
  102. "source": [
  103. "import datashader as ds\n",
  104. "from datashader import transfer_functions as tf\n",
  105. "from datashader.colors import Greys9\n",
  106. "Greys9_r = list(reversed(Greys9))[:-2]"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": null,
  112. "metadata": {},
  113. "outputs": [],
  114. "source": [
  115. "cvs = ds.Canvas(plot_width=plot_width, plot_height=plot_height, x_range=x_range, y_range=y_range)\n",
  116. "agg = cvs.points(df, 'x', 'y', ds.count('z'))\n",
  117. "tf.shade(agg, cmap=[\"white\", 'darkblue'], how='linear')"
  118. ]
  119. },
  120. {
  121. "cell_type": "code",
  122. "execution_count": null,
  123. "metadata": {},
  124. "outputs": [],
  125. "source": [
  126. "import datashader as ds\n",
  127. "from datashader.bokeh_ext import InteractiveImage\n",
  128. "from functools import partial\n",
  129. "from datashader.utils import export_image\n",
  130. "from datashader.colors import colormap_select, Greys9, Hot, viridis, inferno\n",
  131. "from IPython.core.display import HTML, display\n",
  132. "\n",
  133. "background = \"black\"\n",
  134. "export = partial(export_image, export_path=\"export\", background=background)\n",
  135. "cm = partial(colormap_select, reverse=(background==\"black\"))\n",
  136. "\n",
  137. "def create_image(x_range, y_range, w=plot_width, h=plot_height):\n",
  138. " cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)\n",
  139. " agg = cvs.points(df, 'x', 'y', ds.count('z'))\n",
  140. " img = tf.shade(agg, cmap=Hot, how='eq_hist')\n",
  141. " return tf.dynspread(img, threshold=0.5, max_px=4)\n",
  142. "\n",
  143. "p = base_plot(background_fill_color=background)\n",
  144. "export(create_image(x_range, y_range),\"NYCT_hot\")\n",
  145. "InteractiveImage(p, create_image)"
  146. ]
  147. },
  148. {
  149. "cell_type": "code",
  150. "execution_count": null,
  151. "metadata": {
  152. "collapsed": true
  153. },
  154. "outputs": [],
  155. "source": []
  156. }
  157. ],
  158. "metadata": {
  159. "kernelspec": {
  160. "display_name": "Python 3.6 + datashader",
  161. "language": "python",
  162. "name": "datashader"
  163. },
  164. "language_info": {
  165. "codemirror_mode": {
  166. "name": "ipython",
  167. "version": 3
  168. },
  169. "file_extension": ".py",
  170. "mimetype": "text/x-python",
  171. "name": "python",
  172. "nbconvert_exporter": "python",
  173. "pygments_lexer": "ipython3",
  174. "version": "3.6.1"
  175. }
  176. },
  177. "nbformat": 4,
  178. "nbformat_minor": 2
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement