- 
                Notifications
    
You must be signed in to change notification settings  - Fork 7
 
Blog - overview #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Blog - overview #26
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||
| >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() | ||
| ``` | ||
There was a problem hiding this comment.
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.