Skip to content
Open
Changes from 1 commit
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
74 changes: 74 additions & 0 deletions _posts/plotting-overview.md
Original file line number Diff line number Diff line change
@@ -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: <!--read more-->
---
## Altair
Altair uses a declarative grammar. As its [overview](https://altair-viz.github.io/getting_started/overview.html) states:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs slightly more context. Something like: Altair is a Python visualization library built on top of the Vega/Vegalite declarative grammar.

>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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some accidental repetition here


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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe glyph/marker to match matplotlib usage?

```python
mark_point()
```
Then, link your data columns with the encoding channels:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need comma here

```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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this explaination lot! Thought maybe add a sentence/parenthetical on what a canvas is. Or add into the next sentence something like : the general process to create a plot is to first create a figure (canvas) and the subplots (objects containing the images). I'm not liking my explaination of subplots but think it needs something.


The general thought process is to create a plot:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general process to create

```python
fig, ax = plt.subplots()
```
Add a scatter plot to the axes object of this figure:
```python
ax.scatter(x_array, y_array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both the Altair and mpl examples should be runnable, so I think you should make a teensy dataframe at the top to use with both examples. Something like 'df = pd.DataFrame({'x':[1,2,3,4,5], 'y':[4,5,6,7,8])' You can add a throwaway sentence about how we have data we want to plot and python offers us all the options.

```
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()
```