VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-facet-a-scatter-plot-with-altair/

⇱ How To Facet a Scatter Plot with Altair? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Facet a Scatter Plot with Altair?

Last Updated : 3 Jan, 2021

In this article, we will learn how to Facet a Scatter Plot with Altair. Let's recall some concepts :

  • Altair is a statistical visualization library in Python. It is declarative in nature and is based on Vega and Vega-Lite visualization grammars. It is fast becoming the first choice of people looking for a quick and efficient way to visualize datasets. If you have used imperative visualization libraries like matplotlib, you will be able to rightly appreciate the capabilities of Altair.
  • A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data.

Here, we are making the scatter plot using Altair library. For this, we use Chart() function in Altair to load the data and then use the mark_point() function to make a scatter plot. We then use the aesthetics x and y-axis to encode() function. After making this scatter plot we will facet it with a grouped column values such as clusters.

Steps Needed

  1. Import Libraries (Altair).
  2. Create/Load data.
  3. Use Chart() to load data for plot.
  4. Use mark_point() to scatter plot.
  5. Use encode() for x and y axes.
  6. (Optional)Use properties() for setting width and height.
  7. Use facet() over scatter plot with clusters.

Examples 

Let's understand the above-mentioned steps with the help of some examples :

Example 1: 

In this example, we draw a simple facet Scatter plot with some dummy data. That is shown below:

👁 Image

Below is the implementation:

Output:

👁 Image

Example 2 : (Iris Data From Vega Dataset)

Output:

👁 Image

Example 3 : (Cars Data From Vega Dataset)

Output:

👁 Image
Comment
Article Tags:
Article Tags: