Visualisation with Python in Knime

Hello Everybody,

I am looking for a tutorial how to visualise graphics using Python node and seaborn in Knime. Below its a simple example:

import numpy as np
import seaborn as sns
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)

Can someone advice the steps to take in order to visualise a simple heatmap using python node.

Unfortunately Knime doesn’t support a lot of graphics and we need to visualise with python node.

@Angela_dru

I can show you a Plotly solution, similar principles should apply to matplotlib/seaborn.

For the Plotly solution you will need to add the following packages to your Python (conda) environment:

  • plotly
  • python-kaleido

Kaleido is a package to export Plotly charts to static image file formats, and in this example is used to export the Plotly image to png.

The input to the Python View node is a KNIME table with the RowIDs set to the row labels and the column names set to the column labels. e.g.

RowID One Two Three
Mon 12 33 44
Tue 55 66 12
Wed 25 64 54

The Python code is then as follows:

import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio

title = "Total calls per hour per day"
x_label = "Percent of activity"
y_label = "Day of Week"

width = 1280
height = 480

fig = px.imshow(
	input_table,
	labels = {
		"x": x_label,
		"y": y_label
	},
	title=title,
	height=height,
	width=width,
	color_continuous_scale="viridis"
)

# Save image as to output
output_image = pio.to_image (fig, format='png', engine="kaleido")

The heatmap is generated using the Plotly express function imshow. This takes a pandas dataFrame as input (input_table) and displays it as a heatmap. The options should be self explanatory. The final line takes the Plotly figure and exports it back to KNIME as a png file for export on the image port (output_image). Plotly is highly customisable and there are plenty of resources on the internet to show how to adjust various features.

The above code was used to generate the following example output.

I appreciate that it isn’t seaborn but hope that helps.

3 Likes

Thank you @DiaAzul !

I am ashamed to say but I still can’t handle this procedure. I installed python node but i dont understand what i shold do next. Below is my screenshot


python node

@Angela_dru you could check out these examples

https://hub.knime.com/search?type=Workflow&tag=python,graphic&sort=maxKudos

About installing python you might want to read this article

And also consult the official guide

https://docs.knime.com/latest/python_installation_guide/index.html#_introduction

2 Likes

@mlauber71 thank you

When i am installing Python i have an error

please see below, the download remains always at 72%

I do something along the lines of below where “plot” is your sns plot

fig = plot.get_figure()
imgdata = io.StringIO()
fig.savefig(imgdata, format="svg", dpi=300) 
image = [imgdata.getvalue()]

plt.close('all')

output_table = pd.DataFrame()
output_table["Plot"] = image

Then use String to SVG node to show it properly in the table

Hi @Angela_dru,
About the installation problems:
You seem to have the Python Integration installed in one of the screenshots above. What installation is not working now? Note that the " KNIME Python Integration (Labs)" node packages a Conda environment and the installation of this integration can take a bit longer. The errors in your last screenshot’s console have nothing to do with the Python integration but with the Vernalis nodes which is a community extension.

About plotting with the Python Integration:
You can use the Python View node with a script like this:

from io import BytesIO
import numpy as np
import seaborn as sns

uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)

buffer = BytesIO()
ax.get_figure().savefig(buffer, format="svg")
output_image = buffer.getvalue()
4 Likes

Hi @bwilhelm

Thank you very much for your explanation.
I already understood the process. I created successfully a new anaconda environment and already made some graphs.
Its not very complicate but for the first time I was frustrated.

Nice to read that you got it working!

Thanks for your feedback. We are working on making the process more seamless and less frustrating.

  • The “KNIME Python Integration (Labs)” already includes a Python installation → you don’t need to create the Python environment yourself if you don’t have special dependencies.
  • The “Python based KNIME Extensions” have an easy API to create views from plots (no need to write into a BytesIO buffer). Maybe something similar will come for the Python scripting nodes.
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.