Ugly Stool Rotating Header Image

Visualization

LINQPad Charting with Sho

The post, Dumping Charts in LINQPad, is just a start.  There’s actually an easier way to create visualizations in LINQPad by using Sho.  Sho describes itself as the following:

Sho is an interactive environment for data analysis and scientific computing that lets you seamlessly connect scripts (in IronPython) with compiled code (in .NET) to enable fast and flexible prototyping. The environment includes powerful and efficient libraries for linear algebra as well as data visualization that can be used from any .NET language, as well as a feature-rich interactive shell for rapid development. Sho is available under the following licenses: Sho for IronPython 2.6 license and Sho for IronPython 2.7 license. See the Installers page for more details about the two versions.

If you take a small slice of the awesomeness that is Sho, and use it with LINQPad you can create visualizations easily.  The scriptability experience afforded by gnuplot or R is easily duplicated with Sho and LINQPad, however; if you now LINQ the experience is easier and more intuitive in my opinion.  Slice and dice the data in C#, and then pass the IEnumerable<> to Sho for rendering.

To get started, install Sho by downloading and running the MSI.  To run the Sho shell you need to install IronPython, but for these examples, I am only using the Sho libraries so you can skip the IronPython install if you want.

You need to add references to LINQPad for the appropriate assemblies and namespaces.  Download the sample LINQPad script as a starting point – it has all of the references set for you.  (It assumes you installed Sho on a 64-bit machine, so adjust references as appropriate.)

As a first example plot 100 random numbers using Sho.

var chart = new ShoChart();
var random = new Random();
var xs = Enumerable.Range(0, 100);
var ys = xs.Select(x => random.NextDouble());
chart.AddSeries(ys);
chart.Dump();

This results in the following graphs.  (The results will be slightly different between executions because the y values are random.)

image

The amount of code required to dump this chart is small, just three lines (minus the boiler plate).

  1. Construct the ShoChart.
  2. Add a data series to the chart.
  3. Dump the chart.

Sho’s charting can do much more.  Let’s tweak the previous chart a little bit.  Change the line color and style, insert a legend, and add a title.  Overlay a regression line on the chart to better understand the shape of the data.

var chart = new ShoChart();
var random = new Random();
var xs = Enumerable.Range(0, 100);
var ys = xs.Select(x=> random.NextDouble());
var seriesOptions =  new Dictionary<string, object>()
{
    { "color", Color.Red },
    { "linetype", "-.." },
};
var coeff = Poly.PolyFit(xs, ys, 1).Dump();
var regression = Poly.PolyVal(coeff, xs);
var  regressionOptions =  new Dictionary<string, object>()
{
    { "color", Color.Green },
};
chart.Title = "Sho Chart Title";
chart.HasLegend = true;
chart.AddSeries(xs, ys, seriesOptions);
chart.AddSeries(xs, regression, regressionOptions);
chart.SeriesNames[0] = "My Series";
chart.SeriesNames[1] = "Regression";
chart.Dump();

image

This is just some simple line plots, see the Sho documentation for more information, specifically “The Book of Sho.” 

When I pull data from a DB or a OData I usually want to visual it.  I want to understand the shape of my data, and get a better understanding of what it is trying tell me, i.e. graphical analysis.  I do this using either Excel, R, or gnuplot.  Excel is easy to get to from LINQPad, just export, select, and chart.  R and gnuplot are more involved, but create beautiful visualizations. 

Sho means I do not have to leave LINQPad.  I do not have to fiddle with Excel wizards or menus to create graphs.  I do not have the power of R or gnuplot, but enough to get the job done.

I use LINQPad and Sho to create quick reports that I can share with my colleagues.  Use the Dump() functionality of LINQPad to create a table, and use Sho to create an appropriate visualization immediately below it.  Export the LINQPad result window to HTML, convert the HTML to an .MHT file, and viola – a quick a dirty analysis of your data in one file that can be viewed by anyone.

image

I’m doing Sho a disservice by only focusing on the visualization aspects – it can do so much more.  See the documentation to find out what else it can do.

Page optimized by WP Minify WordPress Plugin