A few weeks ago, I wrote a post detailing how to create a multiple variable probit plot. This post improved upon an older post on creating a single variable probit plot. Part of those instructions included adding several “supplemental” lines via Lines and Curves like P10, P90, and the Median. This is actually the most time-consuming part of the process. Each line must be added one by one.
Today, while reviewing my instructions, I realized I had to do better. I know this can be done with IronPython! A quick Google search pulled up this TIBCO community post that I was able to use as a guide. I modified that script to work for my probit plot use case. Now, I have a piece of code that will add all of those lines and is easily modifiable and scalable.
Here is what the code looks like in my DXP. I made the following modifications from TIBCO’s original:
- Changed BarChart to ScatterPlot to suit my visualization
- Modified the expressions from an average to the Percentile, P10, P90, and Median.
- Added code for vertical lines.
Code for Copy & Paste
from Spotfire.Dxp.Application.Visuals import *
scatterPlot = sp.As[ScatterPlot]()
#Add Horizontal Straight Line
horizontalLine1 = scatterPlot.FittingModels.AddHorizontalLine(‘P90([Y])’)
horizontalLine2 = scatterPlot.FittingModels.AddHorizontalLine(‘P10([Y])’)
horizontalLine3 = scatterPlot.FittingModels.AddHorizontalLine(‘Median([Y])’)
horizontalLine4 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],20)’)
horizontalLine5 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],30)’)
horizontalLine6 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],40)’)
horizontalLine7 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],60)’)
horizontalLine8 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],70)’)
horizontalLine9 = scatterPlot.FittingModels.AddHorizontalLine(‘Percentile([Y],80)’)
#Add Vertical Straight Line
verticalLine1 = scatterPlot.FittingModels.AddVerticalLine(’10’)
verticalLine2 = scatterPlot.FittingModels.AddVerticalLine(‘100’)
verticalLine3 = scatterPlot.FittingModels.AddVerticalLine(‘1000’)
- Add a Text Area to the page, right-click, select Edit HTML.
- Click the Add Action Control button.
- Name the button.
- Click the Script button.
- Click the New button.
- Name the script.
- Copy and paste code. Modify to suit.
- Add a parameter called “sp” and connect it to your visualization.
- Run script to test. Click OK to close on script window.
- Modify the HTML as shown to hide the button. You don’t want to click it again.
- Once you run the script, it does not need to be run again. When you clicked Run Script the first time, 13 lines were created. Clicking again will create another 13 lines. I made this mistake when testing. Then, I had to delete a ton of lines one by one! (Please upvote my Idea to allow users to delete more than one line at a time).
- The script creates the lines, but you still have to edit them one by one. This might also be possible with IronPython, but I haven’t dug that far yet.
- If you copy and paste from my code snippet above, you’ll need to replace the quotes. Spotfire won’t recognize them correctly from copy and paste.
This should make setting up probit plots just a little bit faster. You can also modify this code any time you want to add multiple lines to a different visualization or another probit plot.
Content created with Spotfire 7.12.
Guest Spotfire blogger residing in Whitefish, MT. Working for SM Energy’s Advanced Analytics and Emerging Technology team!