diff --git a/.travis.yml b/.travis.yml index c05740d..fa25c6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,8 @@ python: - '3.7' notifications: email: true +before_install: + - nvm install node install: - sudo add-apt-repository --yes ppa:marutter/rrutter3.5 - sudo apt-get update --yes diff --git a/Examples.ipynb b/Examples.ipynb index d70a77f..9bd04d0 100644 --- a/Examples.ipynb +++ b/Examples.ipynb @@ -18,7 +18,7 @@ "\n", "from plotly import figure_factory\n", "from plotly import graph_objects\n", - "import plotly.io as pio\n", + "import plotly.express as px\n", "from IPython.core.magic import Magics, magics_class, cell_magic\n", "\n", "from IPython.display import Image\n", @@ -44,11 +44,10 @@ ") # for plotnine\n", "\n", "\n", - "fig = graph_objects.Figure(layout = dict(width=100, height=100))\n", - "\n", - "templated_fig = pio.to_templated(fig)\n", - "pio.templates['my_template'] = templated_fig.layout.template\n", - "pio.templates.default = 'my_template'\n", + "import plotly.io as pio\n", + "pio.renderers.default = \"png\"\n", + "pio.renderers[\"png\"].width = 750\n", + "pio.renderers[\"png\"].height = 750\n", "\n", "alt.renderers.enable('png', webdriver='firefox')" ] @@ -286,17 +285,10 @@ }, "outputs": [], "source": [ - "mpgGrouped = mpg.groupby('manufacturer').size()\n", - "fig = graph_objects.Figure(layout={'title' : 'Number of Cars by Make'})\n", - "bar = graph_objects.Bar({\n", - " 'type' : 'bar',\n", - " 'x' : mpgGrouped.values.tolist(),\n", - " 'y' : mpgGrouped.index.tolist(),\n", - " 'orientation' : 'h'\n", - " \n", - " })\n", - "fig.add_trace(bar)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " mpg, y=\"manufacturer\", \n", + " title='Number of Cars by Make'\n", + ")" ] }, { @@ -410,13 +402,9 @@ }, "outputs": [], "source": [ - "fig = graph_objects.Figure()\n", - "hist = graph_objects.Histogram({\n", - " 'type' : 'histogram',\n", - " 'x' : mpg['cty'],\n", - "})\n", - "fig.add_trace(hist)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " mpg, x=\"cty\"\n", + ")" ] }, { @@ -547,23 +535,13 @@ }, "outputs": [], "source": [ - "fig = graph_objects.Figure(layout={\n", - " 'title' : 'Engine Displacement in Liters vs Highway MPG',\n", - " 'xaxis' : {\n", - " 'title' : 'Engine Displacement in Liters'\n", - " },\n", - " 'yaxis' : {\n", - " 'title' : 'Highway MPG'\n", - " }\n", - "})\n", - "scatter = graph_objects.Scatter({\n", - " 'type' : 'scatter',\n", - " 'mode' : 'markers',\n", - " 'x' : mpg.displ,\n", - " 'y' : mpg.hwy \n", - "})\n", - "fig.add_trace(scatter)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " title='Engine Displacement in Liters vs Highway MPG',\n", + " labels=dict(\n", + " displ='Engine Displacement in Liters', \n", + " hwy='Highway MPG')\n", + ")" ] }, { @@ -702,7 +680,7 @@ "fig.add_trace(p2)\n", "fig.add_trace(p3)\n", "fig.add_trace(p4)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "Image(fig.to_image(format=\"png\", width=750, height=750))" ] }, { @@ -841,27 +819,13 @@ }, "outputs": [], "source": [ - "traces = []\n", - "for cls in mpg[\"class\"].unique():\n", - " traces.append(\n", - " graph_objects.Scatter(\n", - " {\n", - " \"mode\": \"markers\",\n", - " \"x\": mpg.displ[mpg[\"class\"] == cls],\n", - " \"y\": mpg.hwy[mpg[\"class\"] == cls],\n", - " \"name\": cls,\n", - " }\n", - " )\n", - " )\n", - "fig = graph_objects.Figure(\n", - " layout={\n", - " \"title\": \"Engine Displacement in Liters vs Highway MPG\",\n", - " \"xaxis\": {\"title\": \"Engine Displacement in Liters\",},\n", - " \"yaxis\": {\"title\": \"Highway MPG\"},\n", - " },\n", - " data=traces,\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", color=\"class\", \n", + " title='Engine Displacement in Liters vs Highway MPG',\n", + " labels=dict(\n", + " displ='Engine Displacement in Liters', \n", + " hwy='Highway MPG')\n", + ")" ] }, { @@ -904,7 +868,7 @@ "(\n", " alt.Chart(\n", " mpg,\n", - " title=\"Engine Displacement in Liters vs Highway MPG\",\n", + " title=\"City MPG vs Highway MPG\",\n", " )\n", " .mark_circle(opacity=0.3)\n", " .encode(\n", @@ -942,8 +906,8 @@ " y='hwy', \n", " s=10*mpg['cyl'],\n", " alpha=.5))\n", - "ax.set_title('Engine Displacement in Liters vs Highway MPG')\n", - "ax.set_xlabel('Engine Displacement in Liters')\n", + "ax.set_title('City MPG vs Highway MPG')\n", + "ax.set_xlabel('City MPG')\n", "ax.set_ylabel('Highway MPG');" ] }, @@ -976,29 +940,12 @@ }, "outputs": [], "source": [ - "traces = [\n", - " graph_objects.Scatter(\n", - " {\n", - " \"mode\": \"markers\",\n", - " \"x\": mpg.cty,\n", - " \"y\": mpg.hwy,\n", - " \"marker\": {\"size\": mpg.cyl, \"color\": \"rgba(54,54,54,0.5)\"},\n", - " \"name\": cls,\n", - " }\n", - " )\n", - "]\n", - "\n", - "fig = graph_objects.Figure(\n", - " **{\n", - " \"data\": traces,\n", - " \"layout\": {\n", - " \"title\": \"Engine Displacement in Liters vs Highway MPG\",\n", - " \"xaxis\": {\"title\": \"Engine Displacement in Liters\",},\n", - " \"yaxis\": {\"title\": \"Highway MPG\"},\n", - " },\n", - " }\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"cty\", y=\"hwy\", \n", + " size=\"cyl\", size_max=10,\n", + " title='City MPG vs Highway MPG',\n", + " labels=dict(cty='City MPG', hwy='Highway MPG')\n", + ")" ] }, { @@ -1099,8 +1046,10 @@ }, "outputs": [], "source": [ - "fig = figure_factory.create_facet_grid(df=mpg, x=\"displ\", y=\"cty\", facet_col=\"class\")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " facet_col=\"class\", facet_col_wrap=4\n", + ")" ] }, { @@ -1205,14 +1154,11 @@ }, "outputs": [], "source": [ - "fig = figure_factory.create_facet_grid(\n", - " df=mpg, \n", - " x=\"displ\", \n", - " y=\"cty\", \n", - " facet_col=\"cyl\", \n", - " facet_row=\"drv\"\n", - ")\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.scatter(\n", + " mpg, x=\"displ\", y=\"hwy\", \n", + " facet_col=\"cyl\", facet_row=\"drv\",\n", + " category_orders=dict(cyl=[4,5,6,8])\n", + ")" ] }, { @@ -1362,7 +1308,7 @@ " }\n", " }\n", "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "Image(fig.to_image(format=\"png\", width=750, height=750))" ] }, { @@ -1450,24 +1396,12 @@ }, "outputs": [], "source": [ - "traces = []\n", - "newDiamond = diamonds.groupby(['cut','clarity']).size().unstack()\n", - "for c in newDiamond.columns:\n", - " traces.append(graph_objects.Bar({\n", - " 'x' : newDiamond.index,\n", - " 'y' : newDiamond[c],\n", - " 'name' : c\n", - " }))\n", - "fig = graph_objects.Figure(**{\n", - " 'data' : traces,\n", - " 'layout' : {\n", - " 'barmode' : 'stack',\n", - " 'xaxis' : {\n", - " 'title' : 'cut'\n", - " }, \n", - " }\n", - "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " diamonds, x=\"cut\", color=\"clarity\",\n", + " category_orders=dict(cut=[\n", + " \"Fair\", \"Good\", \"Very Good\", \n", + " \"Premium\", \"Ideal\"])\n", + ")" ] }, { @@ -1558,24 +1492,12 @@ }, "outputs": [], "source": [ - "traces = []\n", - "newDiamond = diamonds.groupby(['cut','clarity']).size().unstack()\n", - "for c in newDiamond.columns:\n", - " traces.append(graph_objects.Bar({\n", - " 'x' : newDiamond.index,\n", - " 'y' : newDiamond[c],\n", - " 'name' : c\n", - " }))\n", - "fig = graph_objects.Figure(**{\n", - " 'data' : traces,\n", - " 'layout' : {\n", - " 'barmode' : 'group',\n", - " 'xaxis' : {\n", - " 'title' : 'cut'\n", - " }, \n", - " }\n", - "})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.histogram(\n", + " diamonds, x=\"cut\", color=\"clarity\", barmode=\"group\",\n", + " category_orders=dict(cut=[\n", + " \"Fair\", \"Good\", \"Very Good\", \n", + " \"Premium\", \"Ideal\"])\n", + ")" ] }, { @@ -1710,7 +1632,7 @@ ")\n", "for d in fig[\"data\"]:\n", " d.update({\"fill\": \"tozeroy\"})\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "Image(fig.to_image(format=\"png\", width=750, height=750))" ] }, { @@ -1773,14 +1695,9 @@ }, "outputs": [], "source": [ - "fig = graph_objects.Figure(layout={'xaxis' : { 'title' : 'date'}})\n", - "scatter = graph_objects.Scatter({\n", - " 'mode' :'lines',\n", - " 'x' : ts.date,\n", - " 'y' : ts.value\n", - "})\n", - "fig.add_trace(scatter)\n", - "Image(fig.to_image(format=\"png\", width=900, height=900))" + "px.line(\n", + " ts, x=\"date\", y=\"value\"\n", + ")" ] }, { @@ -1818,7 +1735,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0" + "version": "3.7.7" } }, "nbformat": 4, diff --git a/INTRO.md b/INTRO.md index 3027c2d..ea93ade 100644 --- a/INTRO.md +++ b/INTRO.md @@ -8,9 +8,7 @@ In particular, I make a lot of bar charts (including histograms), line plots (in Python plotting libraries are manifold. Most well known is Matplotlib. -"[Matplotlib](https://matplotlib.org/ "Matplotlib: Python plotting") is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms." Native Matplotlib is the cause of [frustration](https://stackoverflow.com/questions/tagged/matplotlib) to many data analysts due to the complex syntax. Much of that frustration would be alleviated if it were recognized as a library of lower level plotting primitives on which other tools can be built.[^frustration] - -[^frustration]: If you are frustrated by Matplotlib and haven't read [Effectively Using Matplotlib](http://pbpython.com/effective-matplotlib.html) by [Chris Moffitt](https://twitter.com/chris1610), go read it. +"[Matplotlib](https://matplotlib.org/ "Matplotlib: Python plotting") is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms." Native Matplotlib is the cause of [frustration](https://stackoverflow.com/questions/tagged/matplotlib) to many data analysts due to the complex syntax. Much of that frustration would be alleviated if it were recognized as a library of lower level plotting primitives on which other tools can be built. (If you are frustrated by Matplotlib and haven't read [Effectively Using Matplotlib](http://pbpython.com/effective-matplotlib.html) by [Chris Moffitt](https://twitter.com/chris1610), go read it.) #### Matplotlib-Based Libraries @@ -18,19 +16,16 @@ Many excellent plotting tools are built on top of Matplotlib. [Pandas plots](https://pandas.pydata.org/pandas-docs/stable/visualization.html "pandas documentation") provides the "basics to easily create decent looking plots" from data frames. It provides about 70% of what I want to do day-to-day. Importantly, it lacks robust faceting capabilities. -"[plotnine](https://plotnine.readthedocs.io/en/stable/) is an implementation of a grammar of graphics in Python, it is based on ggplot2." plotnine is a recent attempt to directly translate ggplot2 to Python; despite some quirks and bugs, it works very well for a young product.[^ggpy] +"[plotnine](https://plotnine.readthedocs.io/en/stable/) is an implementation of a grammar of graphics in Python, it is based on ggplot2." plotnine is a attempt to directly translate ggplot2 to Python; despite some quirks and bugs, it works very well for a young product. "[Seaborn](https://seaborn.pydata.org/ "Seaborn: statistical data visualization") is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics." Seaborn makes beautiful plots but is geared toward specific statistical plots, not general purpose plotting. It does have a powerful [faceting utility function](http://seaborn.pydata.org/tutorial/axis_grids.html) that I use regularly. -[^ggpy]: There is also [ggpy](https://github.com/yhat/ggpy "ggplot port for python"), a ggplot2 port, from the company formerly known as yhat. It appears to be abandoned. - #### Interactive Plotting Libraries There are several tools that can make the kinds of plots described here. At present, I have little experience with them. If anyone would like to help add examples, please [get in touch](https://github.com/tdhopper/pythonplot.com). -"[Altair](https://altair-viz.github.io/ "Declarative Visualization in Python") is a declarative statistical visualization library for Python, based on [Vega-Lite](https://vega.github.io/vega-lite/ "Vega-Lite: A High-Level Visualization Grammar for Interactive Graphics")." According to Jake Vanderplas, "Declarative visualization lets you think about data and relationships, rather than incidental details."[^jake] I provide Altair examples rendered as static images. +"[Altair](https://altair-viz.github.io/ "Declarative Visualization in Python") is a declarative statistical visualization library for Python, based on [Vega-Lite](https://vega.github.io/vega-lite/ "Vega-Lite: A High-Level Visualization Grammar for Interactive Graphics")." According to [Jake Vanderplas](https://speakerdeck.com/jakevdp/visualization-in-python-with-altair), "Declarative visualization lets you think about data and relationships, rather than incidental details." I provide Altair examples rendered as static images. -[^jake]: See [here](https://speakerdeck.com/jakevdp/visualization-in-python-with-altair). "[plotly](https://plot.ly/ "Plotly - Make charts and dashboards online")'s Python graphing library makes interactive, publication-quality graphs online. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts." I provide plotly examples rendered as static images. diff --git a/requirements.txt b/requirements.txt index 44159dd..b8b7027 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ ipython pandas jupyter seaborn -plotnine +plotnine<0.7.0 jinja2 jinja2-highlight markdown @@ -12,11 +12,13 @@ scikit-misc boto3 pytest chart-studio -statsmodels==0.10.0rc2 --pre +statsmodels==0.11.1 rpy2 psutil -plotly +plotly==4.10.0 +kaleido tzlocal simplegeneric altair +altair-saver selenium diff --git a/templates/t_index.html b/templates/t_index.html index 01df5d6..2ab1978 100644 --- a/templates/t_index.html +++ b/templates/t_index.html @@ -33,12 +33,8 @@ - - - + - - @@ -58,22 +54,21 @@
-