Plot with 2 y-axes using Python Scripting nodes for KNIME 4.7

Hi Everyone,

I have been learning to use matplotlib in Python for visualisation. There are a few examples already on the hub but from some time ago and therefore using legacy Python nodes. So, I’m just sharing an updated version using the latest Python Scripting nodes, in case they are useful for anyone :slight_smile:

Here is the original matplotlib example, that creates a plot with 2 different y-axes, for example:

You can find my workflow on the hub here:

Hope its useful!
Thanks
Heather

7 Likes

@HeatherPikairos thank you. You could also take a look at this example

2 Likes

Thank you for the initiative to provide additional solutions @HeatherPikairos . I’m sure someone will find it useful.

I was the one asking on the other thread mentioned above regarding the dual axes chart. Been a long way since then - lots of adjustments were made from one version to another.

I started with the dual axes but ended up developing a single axis chart, although I must say it would be nice if I can create a histogram on top of this chart, to replace/be an alternative to the current ‘zones of interest’ lines. That’s something I might look into, and this is just a sharing from what I’m doing (not a request). Glad to know someone else is looking into dual axes graphs on Knime.

2 Likes

Hi @mlauber71 thanks for sharing your solution with me, the plot looks great! I will take a look at the workflow :slight_smile:

Best wishes
Heather

2 Likes

Hi @badger101 thanks for replying. I’m glad you’ve got to something close to what you were looking for! :slight_smile:

Out of curiosity, how do you create the dashed line on the plot instead of a solid line?

Thanks
Heather

2 Likes

@HeatherPikairos That would go under the format strings, I believe. And here’s how I set it up.

ps3

I haven’t had the chance to work on tidying up my scripts yet, because I’m currently preoccupied with the interpretation part of my project. There are some redundancies in this script. I’m sure you can spot them & do it better :grin:

3 Likes

No problem @badger101 :slight_smile: Thanks for sharing this, its always good to learn something new!

2 Likes

As complementary information, I’m sharing the Python code, so people searching for this type of solution on Google can find it more easily:

import knime.scripting.io as knio

from io import BytesIO
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

#Plot creation
fig, ax1 = plt.subplots()

#Input data

data = knio.input_tables[0].to_pandas()

abs = data[knio.flow_variables['abscissa']]
data1 = data[knio.flow_variables['ordinate_1']]
data2 = data[knio.flow_variables['ordinate_2']]

#Vertical label
ax1.set_xlabel(knio.flow_variables['abscissa'])

#First plot color choice
color = knio.flow_variables['First_plot_color']
#Horizontal label
ax1.set_ylabel(knio.flow_variables['ordinate_1'], color=color)
#Creation first plot
ax1.plot(abs, data1, color=color)
#Parameters for first plot
ax1.tick_params(axis='y', labelcolor=color)

# instantiate a second axes that shares the same x-axis
ax2 = ax1.twinx()  

#Second plot color choice
color = knio.flow_variables['Second_plot_color']
#Horizontal label
ax2.set_ylabel(knio.flow_variables['ordinate_2'], color=color) 
#Creation second plot
ax2.plot(abs, data2, color=color)
#Parameters for second plot
ax2.tick_params(axis='y', labelcolor=color)

#Plot resizing
fig.tight_layout()  # otherwise the right y-label is slightly clipped

# Create buffer to write into
buffer = BytesIO()

# write the plot into the buffer
ax2.figure.savefig(buffer, format='svg')

# The output is the content of the buffer
output_image = buffer.getvalue()

# Assign the figure to the output_view variable
knio.output_view = knio.view(fig)  # alternative: knio.view_matplotlib()

Best
Heather

6 Likes

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