From dedd9d036d7060b560fea1596dfe32dc6545e1a8 Mon Sep 17 00:00:00 2001 From: kdorr Date: Wed, 15 Aug 2018 14:51:30 -0500 Subject: [PATCH 1/4] Add altair v matplotlib plot construction overview --- _posts/plotting-overview.md | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 _posts/plotting-overview.md diff --git a/_posts/plotting-overview.md b/_posts/plotting-overview.md new file mode 100644 index 0000000..569eadc --- /dev/null +++ b/_posts/plotting-overview.md @@ -0,0 +1,74 @@ +--- +layout: post +title: "Altair vs Matplotlib Plot Construction Overview" +date: 2018-08-15 14:50:00 -0500 +author: "Kimberly Orr" +categories: about +tags: "intro about matplotlib altair" +excerpt_separator: +--- +## Altair +Altair uses a declarative grammar. As its [overview](https://altair-viz.github.io/getting_started/overview.html) states: +>The key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc.As the overview states, "the key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc." + +So, the general process for creating a basic Altair plot is to specify your data: + ```python +alt.Chart(df) + ``` +Specify what type of glyph/mark should be used to represent your data: +```python +mark_point() +``` +Then, link your data columns with the encoding channels: + ```python +encode(x=alt.X("column1"), y=alt.Y("column2")) + ``` + +So that a finished plot would look like: + ```python +# import +import altair as alt +# plot +alt.Chart(df).mark_point().encode( + x=alt.X("column1"), y=alt.Y("column2"), color=alt.Color("column3") +) +``` +## Matplotlib +Matplotlib is a powerful object-oriented procedural plotting library. Instead of linking data with encoding channels, Matplotlib uses an object-oriented interface to place objects on a canvas. + +The general thought process is to create a plot: +```python +fig, ax = plt.subplots() +``` +Add a scatter plot to the axes object of this figure: +```python +ax.scatter(x_array, y_array) +``` +Show it: +```python +plt.show() +``` + +So that a plot of `y_array` vs `x_array` colored by `color_array` would look like this: +```python +import matplotlib.pyplot as plt +# plot +fix, ax = plt.subplots() +ax.scatter(x_array, y_array, c=color_array) +plt.show() +``` + +## mpl-altair +mpl-altair allows you to create an altair chart as normal and convert/render it as a Matplotlib figure like so: +```python +import altair as alt +import matplotlib.pyplot as plt +import mplaltair +# make an altair chart +chart = alt.Chart(df).mark_point().encode( + alt.X("column1"), alt.Y("column2") +) +# convert to Matplotlib +fig, ax = mplaltair.convert(chart) +plt.show() +``` \ No newline at end of file From dd7220e33f22a8c13b92427f38b03fd4cc3ba220 Mon Sep 17 00:00:00 2001 From: korr Date: Mon, 20 Aug 2018 10:55:02 -0500 Subject: [PATCH 2/4] Wording fixes Still need to make example plots runnable. --- _posts/plotting-overview.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/_posts/plotting-overview.md b/_posts/plotting-overview.md index 569eadc..096b568 100644 --- a/_posts/plotting-overview.md +++ b/_posts/plotting-overview.md @@ -8,20 +8,20 @@ tags: "intro about matplotlib altair" excerpt_separator: --- ## Altair -Altair uses a declarative grammar. As its [overview](https://altair-viz.github.io/getting_started/overview.html) states: ->The key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc.As the overview states, "the key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc." +Altair is a Python visualization library built on top of the Vega/Vegalite declarative grammar. As Altair's [overview](https://altair-viz.github.io/getting_started/overview.html) states: +>The key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc. So, the general process for creating a basic Altair plot is to specify your data: ```python alt.Chart(df) ``` -Specify what type of glyph/mark should be used to represent your data: +Specify what type of glyph/marker should be used to represent your data: ```python -mark_point() +.mark_point() ``` -Then, link your data columns with the encoding channels: +Then link your data columns with the encoding channels: ```python -encode(x=alt.X("column1"), y=alt.Y("column2")) +.encode(x=alt.X("column1"), y=alt.Y("column2")) ``` So that a finished plot would look like: @@ -34,9 +34,9 @@ alt.Chart(df).mark_point().encode( ) ``` ## Matplotlib -Matplotlib is a powerful object-oriented procedural plotting library. Instead of linking data with encoding channels, Matplotlib uses an object-oriented interface to place objects on a canvas. +Matplotlib is a powerful object-oriented procedural plotting library. The general process to create a Matplotlib plot is to first create a figure (canvas) and one or more subplots (which are objects that encapsulate plot data to facilitate adding multiple plots to a single canvas) and then add objects to the subplots (like axes, which contain glyphs, etc.). So, instead of linking data with encoding channels, Matplotlib uses an object-oriented interface to place objects on a canvas. -The general thought process is to create a plot: +The general thought process is to create the canvas/subplots: ```python fig, ax = plt.subplots() ``` From 874909aab0f1d3972fa452bc7df3949ab8b08dc4 Mon Sep 17 00:00:00 2001 From: korr Date: Mon, 20 Aug 2018 16:27:20 -0500 Subject: [PATCH 3/4] Add runnable example --- _posts/plotting-overview.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/_posts/plotting-overview.md b/_posts/plotting-overview.md index 096b568..6df7ec3 100644 --- a/_posts/plotting-overview.md +++ b/_posts/plotting-overview.md @@ -7,6 +7,16 @@ categories: about tags: "intro about matplotlib altair" excerpt_separator: --- + +# Plot Construction Overview +We have data that we want to plot! +```python +df = pd.DataFrame({ + 'x': [1, 2, 3, 4], 'y': [8, 7, 6, 5], 'c': [1, 1, 2, 3] +}) +``` +Fortunately, python gives us options for plotting that data. In this post, we'll look at plotting this data with Altair, Matplotlib, and mpl-altair. + ## Altair Altair is a Python visualization library built on top of the Vega/Vegalite declarative grammar. As Altair's [overview](https://altair-viz.github.io/getting_started/overview.html) states: >The key idea is that you are declaring links between data columns and visual encoding channels, such as the x-axis, y-axis, color, etc. @@ -21,7 +31,7 @@ Specify what type of glyph/marker should be used to represent your data: ``` Then link your data columns with the encoding channels: ```python -.encode(x=alt.X("column1"), y=alt.Y("column2")) +.encode(x=alt.X('x'), y=alt.Y('y'), color=alt.Color('c')) ``` So that a finished plot would look like: @@ -30,7 +40,7 @@ So that a finished plot would look like: import altair as alt # plot alt.Chart(df).mark_point().encode( - x=alt.X("column1"), y=alt.Y("column2"), color=alt.Color("column3") + x=alt.X('x'), y=alt.Y('y'), color=alt.Color('c') ) ``` ## Matplotlib @@ -42,7 +52,7 @@ fig, ax = plt.subplots() ``` Add a scatter plot to the axes object of this figure: ```python -ax.scatter(x_array, y_array) +ax.scatter(df['x'].values, df['y'].values, c=df['c'].values) ``` Show it: ```python @@ -54,7 +64,7 @@ So that a plot of `y_array` vs `x_array` colored by `color_array` would look lik import matplotlib.pyplot as plt # plot fix, ax = plt.subplots() -ax.scatter(x_array, y_array, c=color_array) +ax.scatter(df['x'].values, df['y'].values, c=df['c'].values) plt.show() ``` @@ -66,7 +76,7 @@ import matplotlib.pyplot as plt import mplaltair # make an altair chart chart = alt.Chart(df).mark_point().encode( - alt.X("column1"), alt.Y("column2") + alt.X('x'), alt.Y('y'), alt.Color('c') ) # convert to Matplotlib fig, ax = mplaltair.convert(chart) From ec21f1409311ce07d0d6eeb6e0163bff03c78b3a Mon Sep 17 00:00:00 2001 From: korr Date: Mon, 20 Aug 2018 18:52:04 -0500 Subject: [PATCH 4/4] Update filename to follow Jekyll convention --- _posts/{plotting-overview.md => 2018-08-15-plotting-overview.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename _posts/{plotting-overview.md => 2018-08-15-plotting-overview.md} (100%) diff --git a/_posts/plotting-overview.md b/_posts/2018-08-15-plotting-overview.md similarity index 100% rename from _posts/plotting-overview.md rename to _posts/2018-08-15-plotting-overview.md