Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 360 additions & 0 deletions Examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,174 @@
"sns.histplot(mpg, x='cty', binwidth=2);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Box Plot of a Continuous Value by Category"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:ggplot"
]
},
"outputs": [],
"source": [
"%%R -w 10 -h 10 -u in\n",
"ggplot(data=mpg) +\n",
" aes(x=class, y=hwy) +\n",
" geom_boxplot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:pandas"
]
},
"outputs": [],
"source": [
"mpg.boxplot(column='hwy', by='class',\n",
" rot=45);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:plotnine"
]
},
"outputs": [],
"source": [
"(ggplot(mpg) +\n",
" aes(x=\"class\", y=\"hwy\") +\n",
" geom_boxplot())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:lets-plot"
]
},
"outputs": [],
"source": [
"(lp.ggplot(mpg) +\n",
" lp.aes(x=\"class\", y=\"hwy\") +\n",
" lp.geom_boxplot())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:plotly"
]
},
"outputs": [],
"source": [
"px.box(\n",
" mpg, x=\"class\", y=\"hwy\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:altair"
]
},
"outputs": [],
"source": [
"(\n",
" alt.Chart(mpg)\n",
" .mark_boxplot()\n",
" .encode(x=\"class\", y=\"hwy\")\n",
" .properties(width=400)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:hvplot"
]
},
"outputs": [],
"source": [
"mpg.hvplot.box(y=\"hwy\", by=\"class\",\n",
" rot=45)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:matplotlib"
]
},
"outputs": [],
"source": [
"\"\"\"`boxplot` takes a list of arrays, so the\n",
"groups are split by hand.\n",
"\"\"\"\n",
"groups = mpg.groupby('class')['hwy']\n",
"fig, ax = pyplot.subplots()\n",
"ax.boxplot([v for _, v in groups],\n",
" tick_labels=list(groups.groups))\n",
"ax.set_xlabel('class')\n",
"ax.set_ylabel('hwy')\n",
"pyplot.xticks(rotation=45);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:box-plot",
"package:seaborn"
]
},
"outputs": [],
"source": [
"ax = sns.boxplot(mpg, x='class', y='hwy')\n",
"ax.tick_params(axis='x', rotation=45);"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -2855,6 +3023,198 @@
"source": [
"sns.lineplot(ts, x='date', y='value');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Heatmap of Counts by Two Categories"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:ggplot"
]
},
"outputs": [],
"source": [
"%%R -w 10 -h 10 -u in\n",
"counts <- as.data.frame(\n",
" table(cut=diamonds$cut,\n",
" clarity=diamonds$clarity),\n",
" responseName=\"count\")\n",
"ggplot(data=counts) +\n",
" aes(x=cut, y=clarity, fill=count) +\n",
" geom_tile()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:plotnine"
]
},
"outputs": [],
"source": [
"counts = (diamonds\n",
" .groupby([\"cut\", \"clarity\"])\n",
" .size()\n",
" .reset_index(name=\"count\"))\n",
"(ggplot(counts) +\n",
" aes(x=\"cut\", y=\"clarity\", fill=\"count\") +\n",
" geom_tile())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:lets-plot"
]
},
"outputs": [],
"source": [
"counts = (diamonds\n",
" .groupby([\"cut\", \"clarity\"])\n",
" .size()\n",
" .reset_index(name=\"count\"))\n",
"(lp.ggplot(counts) +\n",
" lp.aes(x=\"cut\", y=\"clarity\",\n",
" fill=\"count\") +\n",
" lp.geom_tile())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:plotly"
]
},
"outputs": [],
"source": [
"px.density_heatmap(\n",
" diamonds, x=\"cut\", y=\"clarity\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:altair"
]
},
"outputs": [],
"source": [
"alt.data_transformers.disable_max_rows()\n",
"(\n",
" alt.Chart(diamonds)\n",
" .mark_rect()\n",
" .encode(\n",
" x=\"cut\", y=\"clarity\",\n",
" color=\"count()\",\n",
" )\n",
" .properties(width=400, height=400)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:hvplot"
]
},
"outputs": [],
"source": [
"\"\"\"hvPlot needs the counts up front; `C` names\n",
"the column that colors each tile.\n",
"\"\"\"\n",
"(diamonds\n",
" .groupby([\"cut\", \"clarity\"])\n",
" .size()\n",
" .reset_index(name=\"count\")\n",
" .hvplot.heatmap(x=\"cut\", y=\"clarity\",\n",
" C=\"count\", rot=45))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:matplotlib"
]
},
"outputs": [],
"source": [
"\"\"\"Matplotlib has no heatmap function, so the\n",
"counts are pivoted and drawn with `pcolormesh`.\n",
"\"\"\"\n",
"counts = (diamonds\n",
" .groupby(['clarity', 'cut'])\n",
" .size()\n",
" .unstack())\n",
"fig, ax = pyplot.subplots()\n",
"mesh = ax.pcolormesh(counts.values)\n",
"ax.set_xticks(\n",
" np.arange(len(counts.columns)) + .5,\n",
" counts.columns, rotation=45)\n",
"ax.set_yticks(\n",
" np.arange(len(counts.index)) + .5,\n",
" counts.index)\n",
"ax.set_xlabel('cut')\n",
"ax.set_ylabel('clarity')\n",
"fig.colorbar(mesh, label='count');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"ex",
"name:heatmap",
"package:seaborn"
]
},
"outputs": [],
"source": [
"\"\"\"`heatmap` colors a matrix, so the counts are\n",
"pivoted into one first.\n",
"\"\"\"\n",
"counts = (diamonds\n",
" .groupby(['clarity', 'cut'])\n",
" .size()\n",
" .unstack())\n",
"ax = sns.heatmap(\n",
" counts, cbar_kws=dict(label='count'))\n",
"ax.tick_params(axis='y', rotation=0);"
]
}
],
"metadata": {
Expand Down
4 changes: 2 additions & 2 deletions INTRO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Put the Matplotlib and ggplot2 versions of the two-variable faceted scatter plot

#### Matplotlib-Based Libraries

[Pandas plotting](https://pandas.pydata.org/docs/user_guide/visualization.html "pandas user guide: Chart Visualization") provides "the basics ... to easily create decent looking plots" from data frames. That is about 70% of what I do day-to-day. It has no faceting, no categorical color mapping, and no smoothing, so five of the examples below have no pandas column.
[Pandas plotting](https://pandas.pydata.org/docs/user_guide/visualization.html "pandas user guide: Chart Visualization") provides "the basics ... to easily create decent looking plots" from data frames. That is about 70% of what I do day-to-day. It has no faceting, no categorical color mapping, no smoothing, and no heatmap, so six of the examples below have no pandas column.

Seaborn calls itself "[statistical data visualization](https://seaborn.pydata.org/ "seaborn: statistical data visualization")." Its classic interface is a set of named functions (`histplot`, `scatterplot`, `countplot`, `lmplot`, `kdeplot`) plus [FacetGrid](http://seaborn.pydata.org/tutorial/axis_grids.html), which I use for faceting more than anything else in the library. It covers every plot below, once you know which function to reach for.

Seaborn 0.12 added [seaborn.objects](https://seaborn.pydata.org/tutorial/objects_interface.html), a second interface built on the grammar of graphics. It composes a plot from marks and statistical transforms instead of dispatching to a named plotting function. The interface has no loess smoother and no regression confidence band, so those two examples are missing.
Seaborn 0.12 added [seaborn.objects](https://seaborn.pydata.org/tutorial/objects_interface.html), a second interface built on the grammar of graphics. It composes a plot from marks and statistical transforms instead of dispatching to a named plotting function. The interface has no loess smoother, no regression confidence band, and neither a box-plot nor a rectangle mark, so four of the examples are missing.

"[plotnine](https://plotnine.org/) is a data visualization package for Python based on the grammar of graphics." It tracks ggplot2 closely enough that most R code translates line for line, down to the `+` for layering. I reach for it when I want ggplot2 semantics without leaving Python.

Expand Down
Loading
Loading