Visualizing Data in Neptyne

Since Neptyne is based on Python, you automatically get access to all the visualization frameworks that come with that language. And since Python is the language of choice for data science, you’ll have an abundance of choices going way beyond what you can do with Google Sheets or Excel. Here we discuss how to create the same graph (a stacked bar graph) using four different tools. It looks like this:

Matplotlib

Let’s start with this granddaddy of visualization frameworks in Python. You’d use matplotlib the same way as you would in Jupyter:

def matplotlib_bar(x_data, y_data1, y_data2):
    fig, ax = plt.subplots()
    ax.bar(x_data, y_data1, color='skyblue')
    ax.bar(x_data, y_data2, bottom=y_data1, color='coral')
    plt.show()

To create a simple stacked plot. We can now call this from the sheet directly with:

=matplotlib_bar(A2:A5,B2:B5,C2:C5)

And it will render whatever is in the A, B and C columns. Note that we call it as if it returns a value, but really that function calls .show(). Neptyne is clever enough to catch the call, convert the show into an image and put the image into the callers cell.

Plotly

Probably the favorite visualization library among the Neptyne crew because of the sheer depth of it. Plotly can do stacked bars too, of course:

def plotly_bar(x_data, y_data1, y_data2):
    fig = go.Figure(
        data=[
            go.Bar(name='Y Data 1', x=x_data, y=y_data1, marker_color='skyblue'),
            go.Bar(
                name='Y Data 2', x=x_data, y=y_data2, marker_color='coral', base=y_data1
            ),
        ]
    )
    fig.update_layout(title='Plotly', barmode='stack')
    return fig

This time we do return the figure. You can call it the same way.

Seaborn

Seaborn is a data visualization library based on matplotlib, but with a focus on statistical data visualization. So the way to return a plot in Seaborn is similar to matplotlib (call plt.show), but you create a dataframe first:

def seaborn_bar(x_data, y_data1, y_data2):
    df = pd.DataFrame({'x_data': x_data, 'y_data1': y_data1, 'y_data2': y_data2})
    df['total'] = df['y_data1'] + df['y_data2']

    sns.set_theme(style="whitegrid")
    ax = sns.barplot(x='x_data', y='total', data=df, color='coral')
    sns.barplot(x='x_data', y='y_data1', data=df, color='skyblue', ax=ax)

    ax.set_title("Seaborn")
    plt.show()

Bokeh

Bokeh is focussed on scientific graphs and has a reputation for producing very pretty results. Of course here we show how the same thing is done in different libraries, so that doesn’t come through all that well. Here’s the code:

def bokeh_bar(x_data, y_data1, y_data2):
    data = {
        'x_data': x_data,
        'y_data1': y_data1,
        'y_data2': y_data2,
    }
    source = ColumnDataSource(data=data)

    p = figure(title="Bokeh", x_range=x_data.to_list(), width=380, height=240)
    p.vbar_stack(
        ['y_data1', 'y_data2'],
        x='x_data',
        width=0.8,
        color=("skyblue", "coral"),
        source=source,
    )
    return p

Pretty straightforward, except for the .to_list() in there. Neptyne cell ranges mostly behave like Python lists, but also mirror the broadcast behavior found in numpy. Which means that you can’t use them in an if statement like:if cell_range: print(“has values”)Normally this is not an issue - cell ranges almost always have at least one value, so you hardly need to check. But calling a third party library with a cellrange can cause problems when that third party library checks for thruthiness.

Conclusion

Neptyne let's you create graphs, plots and maps in practically any python framework available, straight from your spreadsheet. It requires a little coding of course, but it gives you super powers. What you can do with any of the four frameworks discussed here far exceeds what legacy spreadsheets allow you to do.

You can find the code in a tyne below:

https://app.neptyne.com/-/p1jporhpos