Ads Top

Beyond "Show Me" Part 3: Parametric Equations

This is part 3 of a 3 part series on going beyond the charts available in the “Show Me” panel in Tableau. You can read the previous posts here:


In my last post, I provided some background on trigonometry and how we can use it in Tableau. In this post, I’ll build on that and introduce the creation of interesting (and often, beautiful) curves using parametric equations.

Note: If you haven’t read my second post on trigonometry, then I’d highly recommend reading that one first as we’ll be using some trig in this post as well.

What are Parametric Equations?
There are a number of posts available that discuss how to create specific curves in Tableau using parametric equations and I’ll also discuss that in this post, but I want to start by discussing some of the mathematical theory behind these equations.

Unlike the trig of my last post, I honestly never remember learning about parametric equations in high school or college. To be totally honest, I didn’t discover them until earlier this year when I decided to create a Tableau-based valentine for my wife and wanted to draw a heart curve (you can read about that in my blog post, Creating a Heart in Tableau, if you like). So, if you’ve never heard of them before, you’re not alone.

A parametric equation is an equation that calculates values based on one or more independent variables called parameters. Say that again!!

Many of the equations we remember learning in school were ultimately intended to calculate x and y values so you could then plot them on a Cartesian plane.


Examples of these equations are x = 3y and y = 3x2. In these cases, if you have the value of one variable, you can find the value of the other using basic algebra.

Consider the equation x = 3y. When plotted on a Cartesian Plane, you’ll get a simple line that looks like this:



And the equation y = 3x2 will give you a curve like this:



So, as we can see, we are able to express many types of lines and curves through these simple algebraic equations. But, as noted on a tutorial on Paul’s Online Math Notes:

There are also a great many curves out there that we can’t even write down as a single equation in terms of only x and y. So, to deal with some of these problems we introduce parametric equations. Instead of defining y in terms of x () or x in terms of y () we define both x and y in terms of a third variable called a parameter as follows,

x = f ( t )
y = g ( t )

This third variable is usually denoted by t…Sometimes we will restrict the values of t that we’ll use and at other times we won’t. This will often be dependent on the problem and just what we are attempting to do.

Each value of t defines a point that we can plot. The collection of points that we get by letting t be all possible values is the graph of the parametric equations and is called the parametric curve.

Let’s break this down in a bit more detail, starting with each of the components:

  • x – This is the same x we dealt with in the first two posts in this series. It defines the position of a point along the horizontal axis of our Cartesian plane.
  • y – Again, this is the same y as in the first two posts. It defines the position of a point along the vertical axis of our Cartesian plane.
  • t – This is our Parameter, which will be used as part of the equation to calculate x and y.

In the sample equations, x = f ( t ) and y = g ( t ), the letters f and g represent some function. For example, the parametric equations for a circle are:

x = COS(t)
y = SIN(t)

If these look somewhat familiar to you, they should, as they are quite similar to what we used to create a circle in my post on trigonometry. We’ll talk more about that later.

There are a couple of things I’d like to point out about these equations. First, unlike a typical algebraic equation like x = 3y, there are actually two separate parametric equations—one to calculate x and one to calculate y. Second, parametric equations represent curves and calculating curves require the use of trigonometry. Thus, we will typically see Cosine and/or Sine in each equation.

With all this definition out of the way, we can finally talk about how to use them. By using multiple values of t, we can calculate multiple values of x and y. We can then plot those x and y coordinates as points on a Cartesian plane. From there, we can connect them with lines to create an approximation of a curve, called a Parametric Curve. The more values of t you use, the more individual points you’ll get and the more precise your curve will be.

Make sense? If you’re still a bit confused, that’s fine. I didn’t start to understand it completely until I actually created one myself, so let’s do that.

Using Them in Tableau
We’re going to build a circle using the parametric equations we showed above. Unlike my previous post on trigonometry, we’re going to leverage bins in Tableau for this one. I’m not going to dive too deeply into use of bins in Tableau, but there are many good tutorials if you need a bit of background.

1) We’ll start with a very simple data set with one column called Range and two rows with values 1 and 60, which will allow us to create a circle with 60 points. Here’s the data:



Note: In most scenarios, I’d use more than 60 points in order to ensure that the curves don’t look blocky, but for this example, I want to be sure that we can clearly see all 60 points.

2) We’ll bring this into Tableau, then create bins on the Range field with a size of 1.

3) Next, we’ll create a calculated field called Index with the formula “Index()”. When computed using Range (bin), this create 60 values from 1 to 60, which we’ll use for the parameter t in our equations.

4) Just to make our equations very clear, we’ll create one more calculated field called t which has the formula “Index” (we’re just setting it to the same value as Index for now).

5) Now, we can created calculated fields for x and y as follows:

x = COS([t])
y = SIN([t])

6) Next, we’ll drag Range (bin) to the Detail card.

7) Finally, we’ll drop x on the Columns shelf and y on the Rows shelf. This will create table calcs for each. We’ll need to change these to compute using Range (bin).

And now we have a nice circular shape that looks like this:



But there are some problems with this approach. First, as we can see, the points are not evenly spaced from each other in all cases. Second, there doesn’t seem to be any logical ordering of the placement of the points. To demonstrate this, the following shows each point with its associated t value.



The problem with this lack of order is that, if we were to turn the chart into a line, we don’t have any way to order the paths to create a closed circle. If you were to change the chart to a line then drop t onto the path card, you’d get something like this:



It looks pretty neat, but it’s not the circle we’re looking for. The same problem occurs if you try to create a polygon.



To remedy this problem, we’ll need to choose more precise values for t. As it turns out, most parametric equations have a specific range of values that works best for the parameter. In the case of a circle, that range is 0 to 2π. So we want our first point to be 0, our last point to be 2π and everything in between should be evenly spaced. Since we have 60 points, that spacing will be 2π/60 or 0.104719755.

As it stands our first point is t=1 and our last point is t=60, so the easiest way to change t to reflect our new, more precise value is to first subtract 1, then multiply by 0.104719755. Our new formula for t is, therefore:

([Index]-1)*0.104719755

Now when we plot the points, we can see that they are much more evenly spaced:



In addition, if we show the value of t for each point, we’ll see that they are nicely ordered starting at the right and moving counter-clockwise.



Now if we change the chart to a line and place t on the path card, we’ll get a nice closed circle.



And, finally, if we change it to a polygon, we’ll get a solid, filled polygon:



And that’s really all there is to it. With the equations and a few calculated fields, we can easily create a nice circle in Tableau!!

Side Note
A quick side note: As noted earlier, the parametric equations we’ve just used look very similar to what we used to create a circle in my post on trigonometry. The reason for this is that they are essentially the exact same thing. Remember that we divided up the 360° of our circle into equal segments so that our points would be equally spaced. After calculating the angle for each, we then converted the angle to radians before calculating x and y. What we’ve done here is pretty much the same thing. A full arc of a circle, in radians, is 2π. So, where we plotted equidistant points from 0° to 360° in the first post, we’re doing the same thing here when we plot our points from 0 to 2π. Pretty cool, huh!

More Complex Curves
Circles are fairly easy and their equations are quite simple, but parametric equations can be used to plot all kinds of really interesting and beautiful curves. Some of these curves, such as ellipses, can prove to be very valuable in real-life scenarios. Others, while mathematically interesting, will probably only be of aesthetic value.

Take, for example, the famous butterfly curve shown in the header image of this post. By increasing my number of points from 60 to 5,000, setting my t values to range from 0 to 12π, using the following formulas for x and y, and using a polygon chart type in Tableau, I was able to create a slight variation on the butterfly curve.

x = SIN([t4])*(POWER(2.718281828,COS([t4])) - 2*COS(4*[t4]) - POWER(SIN([t4]/12),5))
y = COS([t4])*(POWER(2.718281828,COS([t4])) - 2*COS(4*[t4]) - POWER(SIN([t4]/12),5))

Note: 2.718281828 is actually Euler’s number.



And here are a two more, which I found on the Life Through a Mathematician’s Eyes blog.

t : -8 to 8
x = 6*SIN(13.58*[t])*ROUND(SQRT(COS(COS(7.4*[t]))),0)
y = 6*POWER(COS(13.58*[t]),4)*SIN(SIN(7.4*[t]))



t : -6 to 6
x = 6*SIN(2.94*[t2])*COS(COS(22*[t2]))
y = 6*POWER(COS(2.94*[t2]),2)*SIN(SIN(22*[t2]))



While these formulas seem complex, someone else has already done the math, so it’s really just a matter of translating their formulas into Tableau calculated fields, which can be a bit frustrating, but not terribly difficult.

Some dedicated individuals have even created parametric equations which are capable of drawing famous faces, such as this one of Abraham Lincoln.


Considering the formulas for x and y are both over 13,000 characters long, I certainly don’t have the patience to create these in Tableau. But if you’d like to give it a try, you can find the formulas, in Wolfram Alpha format, here: Abraham Lincoln Curve

Wrap-Up
Well, that’s all for this post and all for the series on going beyond the Tableau “Show Me” panel. There are certainly lots of other chart types which I haven’t addressed in this series, but I hope that you’ve learned something and, perhaps, have been inspired to try something new with this incredible platform we have in Tableau.

Header Image: Wikipedia

Ken Flerlage, November 24, 2017

7 comments:

  1. That is awesome! Learning tableau by doing math or learning math by creating Tableau, either way I love such exploration.

    ReplyDelete
  2. Terrific series, thank you again. This is definitely enough to help people get started in going beyond the canned chart types!

    ReplyDelete
  3. Brilliant series, Ken. I've been looking for something like this for a while. I've gotten in the habit of downloading public vizzes and exploring them over the last few year and have found the great vizzes always have a bit of complex math driving the view. It has often seemed that the math is what is keeping me from moving my Tableau game to the next level. This will help a great deal. Truly appreciate the insights.

    ReplyDelete
    Replies
    1. I'm so happy you found this helpful, Michael!

      Delete
    2. BTW. Take a look at my latest blog post. In it, I actually put parametric equations to work!

      Delete

Powered by Blogger.