DATA STORIES | DATA VISULIZATION | KNIME ANALYTICS PLATFORM

Exploring the Power of Python Graphics with KNIME: A Collection of Examples

TL;DR: Python graphics made easy with KNIME’s low-code approach. From scatter, violin and density plots to PNG files and Excel exports, these examples will help you transform your data into actionable insights

Markus Lauber
Low Code for Data Science
6 min readApr 1, 2023

--

Photo by DeepMind on Unsplash.

First you can revisit my blog about how to set up a KNIME component and use Python to make a dashboard:

In this blog I would like to present a collection of examples to show you what you can do beyond the usual KNIME graphic environments *1). Sometimes the range of options can be a bit overwhelming. But if you master a few lines of Matplotlib or Seaborn Python code, you will go a long way as we will see.

And as always with KNIME: you will have the data, a lot of functions/nodes and all the exports as well as the workflow itself (think of automation) at your fingertips along with the stunning charts…

KNIME is working on a new set of JavaScript-based visualization nodes, with a higher degree of interactivity and responsiveness: New Visualization Nodes in KNIME (Labs).

Interactive Graphics with Python

KNIME has recently introduced a new set of ‘interactive’ graphics with the Python View node which sound quite interesting. Currently, the interactivity refers to the output of non-static images and that the node can synchronize the selection of data points with other views in KNIME (when used in a component). Additionally, you could also use the node to export the graphics in PNG (or SVG) format. How about some maps:

Example of an interactive graphic with KNIME
Example of an interactive graphic on a map (https://forum.knime.com/t/interactive-native-python-node-really-possible/64136/6?u=mlauber71).

You can read more about Python interactive views here: All Python-based Visualization Libraries Easily Accessible through KNIME and see more examples on the Python Script Space.

Note. the code examples here are often from the sample collections of Matplotlib and Seaborn or other instructive sites. The sources are linked beneath the KNIME workflows on the Hub. Some code has been developed and discussed with the help of ChatGPT: “KNIME, ChatGPT and Python”.

Chart with two different Y-Axis

What if you have a large number of entries (like a time series) and two y-axis? And you want the graphic to auto adapt the scales and the time-axis, and also be able to control the colors. The result could be something like this:

Two different Y-axis
Two different Y-axis on a Timeline (https://forum.knime.com/t/hybrid-graph/64012/4?u=mlauber71).

As with most examples in this blog, you can control the settings so as to re-use the format with your own data with little effort. You can also have a headline and a footnote and edit the axis text all within a KNIME component:

Control two different Y-axis with a KNIME component (https://forum.knime.com/t/hybrid-graph/64012/4?u=mlauber71).

Scatterplot with continuous hues and sizes

These approaches work best if you have a good example, your graphic task might not fit the standard KNIME nodes, and you might have an idea to re-use such an example in the future. Here is an example with Seaborn using the planets dataset:

Planets by their distance, orbital period and mass as well as the year of discovery (https://hub.knime.com/-/spaces/-/latest/~lR1c8hVkM0UZcABc/).

We see that in more recent years planets have been discovered with larger masses in far greater distances and also their orbital period seem to increase. Maybe our ability to detect them is getting better. Please note: the scale has been set to log() in order to better spread out the values, but you can adapt that in the settings.

The inspiration for this approach came when I did some charts for a book of a friend of mine about statistics between Germany and Austria. For the book, the code for each chart was ‘hand crafted’ with annotations and so on:
https://makronom.de/statistisch-gesehen-eine-ode-an-das-erbsenzaehlen-36090

How about a Kernel Density Plot for Penguins?

One example of a kernel density plot shows the combination of two numbers characterizing different species of penguins by the length and depth of their bill. While individual values overlap (see the charts on the margins), if you combine them you can ‘see’ the different species of penguins in the data:

You can see the different species of Penguins
You can see the different species of Penguins (https://hub.knime.com/-/spaces/-/latest/~m0D0BILjHyH751Cd/).

The workflow allows for three types of plots to be created and exported. Here you see the same information displayed as a histogram (scatter plot would be the next option):

Penguin bills as histogram
Penguin bill length and depth as histogram (https://hub.knime.com/-/spaces/-/latest/~m0D0BILjHyH751Cd/).

The name of the exported PNG file will automatically adapt to the type of plot selected.

You can do 3D Graphics

So even if you have some (very) special wishes, as long as there is a Python code example out there (or you let ChatGPT create one), you most likely will be able to create a chart with the help of KNIME and control the text and data, like doing a 3D graphic:

A 3D graphic with numbers of COVID-19 cases from Brazil
A 3D graphic with numbers of COVID-19 cases from Brazil (https://forum.knime.com/t/3d-bar-chart/46151/3?u=mlauber71).

All entries can also be controlled by KNIME Flow Variables, which can be created based on your data, so you have a lot of options to steer the graphics creation with your usual KNIME nodes and automations.

Seaborn Line Plots with 2 Categories using FacetGrid

Having more than one element on a chart is a recurring request on the KNIME Forum. Here we have a graphic where two groups of line-plots are compared by a class/string variable. We see a comparison of wages in industrial and information jobs over the years clustered by education — it seems “advanced degree” is loosing income in the industrial sector — or people leaving it or jobs being taken by different education levels — you can figure it out yourself, but well you now have the figures…

Wages in sectors compared over the years and education levels
Wages in sectors compared over the years and education levels (https://hub.knime.com/-/spaces/-/latest/~E_k-EiJt_jS8PK4w/).

Export PNG images directly to Excel

Another bonus of the Python integration in KNIME is that you can export the charts as PNG files but also put them into your Excel file without additional Conda installations:

Export your PNG graphic directly to Excel
Export your PNG graphic directly to Excel (https://forum.knime.com/t/reading-jpg-files/60934/3?u=mlauber71).

A grouped violin plot of restaurant bills on various weekdays exported from KNIME straight into Excel:

A grouped Violin Plot inside Excel
A grouped Violin Plot inside Excel (https://hub.knime.com/-/spaces/-/latest/~ELPUWIfObOx32uLk/).

It seems smokers on a weekend overall spend more, though there is a spread. For the non-smoker Friday seems to be special — or at least was in a time when you could still smoke in restaurants…

There is more with KNIME…

I hope you enjoyed this tour of advanced graphics with Python and KNIME. If you want even more examples you can check out my repository on the KNIME Community Hub (https://hub.knime.com/mlauber71):

Overview of KNIME Graphic integrations and nodes

*1) I have collected this list of graphic environments that are supported by KNIME — some might argue this is a long list. But here you have it anyway:

And there is even more to explore: Awesome Visualisations In KNIME Using Python Plotly.

--

--

Markus Lauber
Low Code for Data Science

Senior Data Scientist working with KNIME, Python, R and Big Data Systems in the telco industry