Day 2: Seeing names with Altair

Welcome to class!

Gratitude Journal

Announcements


Methods Checkpoint


Getting started with Altair

Grand Grand Question 1

What does a chart need to look like to answer Question 1?

What data do we need to build that chart?


Making our chart look good.

  • Size of chart
  • Title and subtitle
  • Size and color of line
  • Axis formatting
  • Reference marks

Extra Practice

What is the difference between a “high-level” and “low-level” programming language or tool?

Here’s what Google has to say.

  • Altair is a Python library built on Vega and Vega-Lite
  • Vega is a “higher-level visualization specification language on top of D3” that creates charts with json files
  • D3 is a JavaScript library

Remember, Altair builds on Vega, which builds on D3.

Sometimes to answer a question about Altair, you will have to read Vega or D3 documentation. For example:

(alt.Chart(my_data)
   .mark_line()
   .encode(
      x = alt.X('year', axis = alt.Axis(format = 'd', title = "Year")), 
      y = alt.Y('Total', axis = alt.Axis(title = "Children with Name"))
    )
)

You may want to include a point or line of reference to help your chart answer the question “compared to what?”.

Let’s say you have your chart for Grand Question 1 saved as question_1. The easiest way I have found to add a reference line is to create a new DataFrame with a single number:

line_df = pd.DataFrame({'year': [1990]})
line_df

And use the new DataFrame to create a chart with a single line that has a specific value of x (for example, your birth year) but spans the entire y-axis.

In Altair, this is done with the the mark_rule() geometry. You can then “layer” the two charts together.

line = alt.Chart(line_df).mark_rule(color="red").encode(x = "year")

final_chart = question_1 + line
final_chart

Additional references: