Tableau 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 tbe 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 xand 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 Rangefield with a size of 1.

3) Next, we’ll create a calculated field called Indexwith 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 Indexfor 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 tonto 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 ton 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

27 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
  4. Thanks for this series and breaking it down like this! I found it quite insightful, to understand how these interesting custom shapes are created. Still don't quite understand why Tableau was creating the shapes using line and polygon in the beginning before t was restricted [0,2π]. Also, i was following your post to create the circle and noticed that i couldn't get the circle to close after making t =([Index]-1)*0.104719755, unless I increased the max range from 60 to 61. Not sure if it was implied in the post or I did something wrong. Anyways, it was a very insightful post and i love to look at the old math and see how they could be used in art and tableau! thanks again for opening my eyes!

    ReplyDelete
    Replies
    1. On the first point, picking random t values will scatter the dots around the circle in ways that don't line up properly. So Tableau will connect the dots in that order and create something weird.

      I think you're right about closing the circle. You will need to duplicate that first point to get it to work. Good catch!

      Delete
  5. I am little blown by the "Beyond Show me..." Series (literally like the emoji of #WorkOutWednesday).

    First of all thank you so much Ken, You have articulated the series so well that anyone would understand the concept.
    Although I am just a newbie and still discovering the "Show Me" section but I am super exited to use these concepts in upcoming viz.
    Thank you so much :)

    ReplyDelete
  6. Thanks for series and very detailed explanation. It's really basic ideas which bother me for a long time .Frankly speaking, every time I download a workbook contains sin,cos,pi I will definitely close it. However, now I will try to understand the logic inside. Thank you Ken!

    ReplyDelete
  7. Thanks for sharing these useful techniques. However,I got some trouble to draw the picture.

    I use your method to draw the circle,it works at the first worksheet in tableau.But when I draw the

    same circle using the same method in the second worksheet, it failed. I have no ideal what cause this

    problem. Please let me know if you have the solution,thank you.

    ReplyDelete
    Replies
    1. I'd have to see the workbook. Can you share it with me? flerlagekr@gmail.com

      Delete
  8. Hello Dear Ken, I am a novice and we learn from Masters like you. I have a question: How would I do the range for t for -8:8 as shown in the " And here are a two more, which I found on the Life Through a Mathematician’s Eyes blog." - I tried everything but I probably missed the obvious - I also tried doing : ([Index]-1)*(-8*(22/7)/5000) and ([Index]-1)*(8*(22/7)/5000) and then tried a Dual Axis - but the shape is not coming like yours - apologies for some obvious ignorance. If you could please help.

    ReplyDelete
    Replies
    1. Might be easier to discuss this offline. Can you send me an email? flerlagekr@gmail.com

      Delete
  9. It was a marvelous piece of Advance tableau charts , really appreciate your work.

    ReplyDelete
  10. wow Ken these 3 series is amazing.... such a wonderful tool for sparking kids interest in math.... how did you go about the Afghan Girl animation using lines?!!! dying to learn more ... please!

    ReplyDelete
    Replies
    1. The trick is all in an algorithm I found on github. Take a look at the following for more details https://www.kenflerlage.com/2017/11/loom-art-in-tableau.html?m=1

      Delete
  11. Hi Kevin & Ken! I studied your 3 articles on how to draw lines, circles etc. in beyond "Show me". Excellent work! May God bless you! 🙏❤️😀

    ReplyDelete
  12. A bug in Tableau – Broken bin:
    Summary: The bins may break when upgrading to a newer version of Tableau.
    Fix: Recreate the bin and replace the old one.

    Details:
    Source workbook file = v 10.4 (which is what is used in this post as of 2020-04-26 - Your posts are FANTASTIC!! THANK YOU!!)
    Tableau version = 2020.1.20.0326.1623 – 64-bit Professional Edition
    Actions:
    1: Download the workbook in this post (version 10.4)
    2: Open the workbook with Tableau v2020.1.20.0326.1623 – 64-bit Professional Edition
    3: Unhide the Dashboards in the Story
    4: Unhide the worksheets in Dashboard #3
    5: Look at worksheet ‘S’, you are going to recreate this using a NEW worksheet. Do not duplicate any existing worksheets in the workbook. Click the ‘new worksheet’ icon on the far right.
    6: Drag ‘Range (bin)’ to details card
    7: Drag ‘x’ to the Columns shelf
    8: Drag ‘y’ to the Rows shelf
    9: Set ‘x’ and ‘y’ to ‘Compute using Range (bin)’
    10: What you should see is a circle of dots… but it will not happen…
    If you go to the menu item ‘Worksheet’ and ‘Describe Sheet’ you will see that ‘Range (bin)’ only has two members: ‘0 ;60’. This is the problem
    To fix this:
    11: Right click the measure:‘Range’ and create a new ‘Range (bin) 2’ Dimension.
    12: Replace the 'Range (bin)' that is currently on the ‘Details’ card with the one you just created. You will have to fix ‘x’ & ‘y’.
    That should get the circle to work.
    I am now going to find some Band-Aids to put on my forehead and buy a new keyboard.

    ReplyDelete
    Replies
    1. See my solution here: https://community.tableau.com/thread/338745. Basically, you just need to drag the bins pill to the rows or columns shelf, then right click and choose 'Show Missing Values' then drag back to the detail card.

      Delete
    2. Thanks Ken and Ken. It took me two days to scroll and see this. I was struggling to find where I was wrong when I was following all steps.
      Harish

      Delete
  13. *** Sorry, I got to excited... that may not be a solution to the problem..... I need to look at it more to verify....

    ReplyDelete
  14. **** Yes, the process I posted is correct..

    ReplyDelete
  15. I have successfully plotted the points in a circle and adjusted T to account for equal spacing. However, when I change the mark from automatic to line, I get a sinusoidal wave in the shape of a circle instead, and the polygon mark shows a circle with alternating spikes protruding from the horizontal middle. Is there a way to fix this?

    ReplyDelete

Powered by Blogger.