Skip to main content

#Getting0 personne en discute

Please see this post as a reference: How to create a smoothed line chart with Tableau & Python (detailed guide)

 

I want to smooth a jagged line - I know why most people recommend not doing this, but I am required to do this.

 

I attempted to apply this method to my data, but I believe the issue is with my data - so I was hoping to figure out how to fix it so that it wouldn't be an issue anymore. I just want to plot two quantities as a line graph, right now their name are [BT_Ceiling] and CNT([F7]).

[BT_Ceiling] just applies a ceiling function to the values from [BT] (which is in my original data) - the range for this is all the whole numbers in [0,24] inclusive.

CNT([F7]) is expressed as a percentage and count the number of data points that falls in each "bin" (for lack of a better word) based on the range created from [BT_Ceiling].

 

The code I attempted to use for the smoothing function is as follows:

SCRIPT_REAL('

import scipy.interpolate as interpolate

 

X_all=_arg1 ⌗assigning date dimension to x axis

y_all=_arg2 ⌗assigning our KPI measure to y axis

 

y=[i for i in y_all if i is not None] #getting rid from Nones for y

X=[i[0] for i in enumerate(X_all) if i[1] is not None] #getting list of indexes for X without Nones

 

Xsmooth=list(range(0,len(X_all))) ⌗generation full list of indexes for X

ysmooth=interpolate.pchip_interpolate(X,y,Xsmooth) ⌗interpolation (here i used pchip, but depending on source data, other function can be used, like spline)

 

result=[round(i,10) for i in ysmooth] #rounding to fit in output JSON

return result

', FLOAT([BT_Ceiling]),COUNT([F7]))

 

I am having an issue with the bolded sections since the data is aggregate and non-aggregate. I've also attached a packaged workbook f what I am working with. I am running Tableau 2018.2.3.

 

Thanks in advance!

4 réponses
  1. 29 déc. 2018, 22:49

    Hi Kara,

     

    In the document you've been referred to

    there is a notion of using a 'range-aware' Pill --

    such as a Date/Datetime type or a Bin one --

    to generate the additional 'densified' Marks on a view,

    for which the interpolated values could be plotted.

     

    I've been using a Tableau-generated Bin (Discrete aka Blue) Pill.

     

    The flag Show Missing Values set for this (range-aware) Pill

    is triggered the data padding (creating more Marks) on a view.

     

    Please find the attached.

     

    Yours,

    Yuri

0/9000
How to create a smoothed line chart with Tableau & Python (detailed guide)First of all, DON'T DO THAT!

Why? Read Andy's post: When you use a smoothed line chart, your data is not affected, it’s misrepresented!

Try to rethink and negotiate requirements, clearly understand the pros & cons of such approach!

 

The task:

Let's say you have some KPIs that you would like to see on a timeline

If that's a monthly report, your data set could look like this:

pastedImage_4.png

 

Based on this data you would like to create a nice looking chart, like following:

pastedImage_7.png

No? you need a smooth line? Please tell me why in the comments!

Using Tableau convenient features you can't achieve a smooth line (which is mathematically incorrect, but who cares )

 

To create a chart with curved line you need to generate a new data set with a higher density - that's it!

Densification in Tableau can be done in two ways:

- If you have a dimension with missing values (like in above data set days are missing) just click  'Show Missing Values' on date dimension - this will create Nulls for all missing dates

- In case there are no missing values, its possible to add them by creating a Bin based on measure you're going to use as X axis and then click 'Show Missing Values'

Generation of Y axis based on densified X axis is just a simple interpolation (for Python you can see info here: https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html)

 

Let's go step by step:

0.  Install and connect to TabPy server, instructions and zip available here: GitHub - tableau/TabPy: Execute Python code on the fly and display results in Tableau visualizations

To connect use: Tableau: Help > Settings and Performance > Manage External Service Connection

1. Connect to data and put Date dimension on Columns (you should use continuous value of the Day level)

2. Right click on Date dimension and select 'Show Missing Values'

pastedImage_9.png

3. Create calculation field with Python script as follows:

 

SCRIPT_REAL('

import scipy.interpolate as interpolate

 

X_all=_arg1 ⌗assigning date dimension to x axis

y_all=_arg2 ⌗assigning our KPI measure to y axis

 

y=[i for i in y_all if i is not None] #getting rid from Nones for y

X=[i[0] for i in enumerate(X_all) if i[1] is not None] #getting list of indexes for X without Nones

 

Xsmooth=list(range(0,len(X_all))) ⌗generation full list of indexes for X

ysmooth=interpolate.pchip_interpolate(X,y,Xsmooth) ⌗interpolation (here i used pchip, but depending on source data, other function can be used, like spline)

 

result=[round(i,10) for i in ysmooth] #rounding to fit in output JSON

return result

', attr([Date]),ATTR([Value1]))

 

4. Put Calculation with Python script on Rows

 

Use the attached workbook to see how it works (Install TabPy first)

Workbook, data set and script are available as well here: GitHub - KRLY05/Tableau: TabPy scripts

P.S. This approach can be used for much more valuable things than just a smooth line, so follow these links to get more inspirations:

Quick Tip : Overlaying curves on Tableau scatter plots with R | Bora Beran

Curved Lines in Tableau through Data Densification | The Last Data Bender

10 réponses
  1. 14 août 2018, 10:07

    Hello!

     

    I like this description, it is very helpful, I tried to make the mentioned settings, but unfortunately it did not work. I have got this below error message, after I clicked to 'Test Connection'

    Hello! I like this description, it is very helpful, I tried to make the mentioned settings, but unfortunately it did not work.

    Do you have any idea, why I get this, and could you please advise, how I can solve this issue?

    Thanks in advance.

    Agnes

0/9000