Loading video player…

Creating Your First Chart

00:00 In the previous lesson, you installed Altair, and so in this lesson, it’s time to create your first chart. The steps to creating an Altair chart are: first, import the Altair library.

00:11 You actually did that in the last lesson. The next step is to load some data. In this course, we will use some data about penguins. Let’s do that now. In the notebook, you already imported altair as alt using the preferred alias.

00:25 The next step is to actually import some data from the Altair library itself. You can use external data sources as well, of course. The import statement is from altair.datasets, which is a collection of data built into the Altair library, import data, run that cell, and then create a variable called df, which is equal to data.penguins(), which is one of the built-in datasets in Altair.

00:58 Once that worked, you can check the type of this df variable, and it is a pandas DataFrame. Like many visualization libraries in Python, Altair likes to work with pandas DataFrames.

01:10 Let’s take a look at this data by calling the .head() method. This is the data you’re going to be working with. It’s a series of measurements about different penguins, their species, which island they’re on, and some measurements around their beaks and flippers and body mass.

01:26 Now you have some data loaded, it’s time to visualize it. You’re going to create an Altair chart by specifying three components, the data, the marks, and the encoding.

01:37 These three components are actually the crucial components of any Altair chart. You start by specifying the data that will be visualized, then you will specify something called the marks, which determine what kind of shapes you want in your final visualization.

01:52 Is it going to use points, bars, lines, or something else? And finally, you specify the encoding, which is how to map different columns of data onto your marks.

02:03 This keeps the focus on your output visual, how your data maps to the final visualization, rather than the ins and outs of actually how to visualize it. So the components of the code are creating a chart object, creating a mark, and encoding your data to those marks.

02:22 Let’s go ahead and do that. Back in the notebook, you’re going to type alt.Chart() to create a new chart object, passing in the df variable as the dataset.

02:33 Then the next step is the mark. In this case, the method is called .mark_point(). Then you’re going to map this data onto these marks by using the .encode() method.

02:44 And inside the .encode() method, you’re going to say x, which is what data goes on the x-axis, is going to equal a string, which represents one of the column names.

02:55 In this case, you’ll use Body Mass,

02:59 which also has lowercase g in brackets. You have to be careful to use the exact column name from the pandas DataFrame. And then the other thing you need to specify is which data column maps to the y-axis.

03:12 In this case, use Flipper Length in millimeters, taking care to use the exact column name from the DataFrame. When you run this code and scroll down in your notebook, you’ll see the output chart.

03:25 You’ll see that Body Mass is on the x-axis, Flipper Length is on the y-axis, and each penguin represents a point. All you had to specify was data, the marks, and the encoding.

03:38 The creation of the axis, the labeling, all of that was handled by Altair.

03:44 Another thing that’s useful in data analysis libraries is to make sure that your columns are in the correct types. This means that your data analysis or visualization library won’t make a mistake in how to handle your columns.

03:58 Altair, like many other data libraries, can actually infer your data types by looking at the values in your columns, but if you’re explicit about which column is what type of data, you can avoid surprises.

04:10 The way to specify this in Altair is to put a colon and a letter after the column name when you use the .encode() method. For example, a colon and a capital Q tells Altair that this column is numeric.

04:24 A capital N tells Altair this column is nominal, which means it’s categorical, but there’s no natural ordering to the values, such as the species of penguins.

04:34 Capital O tells Altair this data is ordinal, which means it’s still categorical, but there is a natural ordering to the categories, such as T-shirt sizes: small, medium, and large.

04:45 And a capital T tells Altair this column is temporal, which means it’s a date or a time, such as the date that a record was created.

04:54 So to keep with best practice, you can update your code to add :Q to the end of column names because they’re both numeric columns. Because Altair has correctly inferred your data types in the first place, running this updated code won’t actually change the output, but it’s in keeping with best practices.

05:14 So that’s your first Altair chart. In the next lesson, we will talk about more encoding channels and how we can map more data to your visualization.

Become a Member to join the conversation.