Skip to content

Instantly share code, notes, and snippets.

@cedarwood-insights
Last active February 19, 2021 16:32
Show Gist options
  • Save cedarwood-insights/dc6e62d162fc12c454c78e29060de230 to your computer and use it in GitHub Desktop.
Save cedarwood-insights/dc6e62d162fc12c454c78e29060de230 to your computer and use it in GitHub Desktop.
Example python code used for the KNIME Python View node to demonstrate plot image generation using Plotly and Kaleido
# Import modules and functions
import plotly.express as px
from plotly.offline import plot
# Apply short name to input_table dataframe
df=input_table
# Define category labels
categoryList=list(df.columns)
# Assign X as “Week No”
X = categoryList[0]
# Assign Y as the other columns
Y = categoryList[1:]
# Define the plot
fig=px.area(df,x = X, y = Y,
title = "Total deaths by Health Board",
labels={"value": "No of deaths", "variable": "Health Board"})
# Launch plot in the browser
plot(fig, auto_open=True)
# Save image as interactive HTML
fig.write_html("../../Images/stacked_area_plot_weekly_total_deaths.html")
# -------------------------------------
# Use Kaleido to generate the following static images
# Plotly will attempt to use Kaleido by default so no real need to specify engine.
# -------------------------------------
# Save image as static png
fig.write_image("../../Images/stacked_area_plot_weekly_total_deaths.png", engine="kaleido")
# Save image as static jpeg
fig.write_image("../../Images/stacked_area_plot_weekly_total_deaths.jpeg", engine="kaleido")
# Save image as static svg
fig.write_image("../../Images/stacked_area_plot_weekly_total_deaths.svg", engine="kaleido")
# Save image as static pdf
fig.write_image("../../Images/stacked_area_plot_weekly_total_deaths.pdf", engine="kaleido")
# Generate KNIME output_image
# -----------------------------------------
# Load Plotly IO module
import plotly.io as pio
# Convert figure to a static image bytes string and set output_image to this.
output_image = pio.to_image (fig, format='png', engine="kaleido")
# -----------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment