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
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:
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:
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:
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:
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:
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):
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:
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…
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:
A grouped violin plot of restaurant bills on various weekdays exported from KNIME straight into Excel:
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):
- More Python graphics on the KNIME Community Hub
- Upcoming is another collection of graphics with R/RStats, so watch out — or check this initial example: “The beautiful Violin Plot that has it all” — Create Graphics with R and KNIME Components
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:
- Basic integrated graphic nodes called “Local (Swing)” (https://hub.knime.com/search?type=Node&tag=Local%20%28Swing%29&sort=maxKudos)
- KNIME JavaScript View (https://kni.me/e/0qveOxOA3751S6Vx)
- KNIME Plotly integration (https://kni.me/e/NXfbxRi-JV1Tt7yC)
- KNIME JFree Charts (https://kni.me/e/aUkjvZvAcB3qSeD8)
- Generic JavaScript View (https://kni.me/n/YQOLY5hCc4m9Opmd) — to be honest this is quite complicated to handle as you can see in this extensive example (https://kni.me/w/mG4KOD0vW32FEpeD)
- NEW 4.6 visualization nodes (https://www.knime.com/whats-new-in-knime-46#vis-nodes)
- KNIME ‘interactive’ Python Views (https://www.knime.com/blog/access-to-python-visualization-libraries)
- Special nodes from the Community (https://kni.me/n/RLQEAvvngtuUl8WW) like the Notched Boxplot (JFreechart) node
- NEW in v.4.7 geospatial visualization nodes from the Community (https://hub.knime.com/center%20for%20geographic%20analysis%20at%20harvard%20university/extensions/sdl.harvard.features.geospatial/latest/) like the Geospatial View, Kepler.gl Geoview and Spatial Heatmap nodes.
And there is even more to explore: Awesome Visualisations In KNIME Using Python Plotly.