Creating plots in a task type

In Python for example Example.Basic.PlotXY uses Core.Python.matplotlib and Core.Python.seaborn to generate a simple plot:

# Avoid requiring tkinter
import matplotlib
matplotlib.use('agg', force=True)

import pandas
from pandas.api.types import is_numeric_dtype # type: ignore
import seaborn

import xfworkerutil


worker = xfworkerutil.XFWorkerJob()

inputs = worker.job['inputs']
x_column_name = inputs['x_column_name']
y_column_name = inputs['y_column_name']
group_column_name = inputs['group_column_name']
mode = inputs['mode']

if group_column_name is not None and mode != 'basic':
    worker.error("Grouping is only supported in basic mode.")

# Download input files
table_path = worker.download_input_file(inputs['table'])

plot_path = worker.create_related_path(table_path, '_plot.png')

# Plot
data = pandas.read_csv(str(table_path))

if x_column_name is None:
    x_column_name = [name for name in data.columns if name != y_column_name and is_numeric_dtype(data[name])][0]
if y_column_name is None:
    y_column_name = [name for name in data.columns if name != x_column_name and is_numeric_dtype(data[name])][0]

if mode == 'ci+univariate':
    plot_grid = seaborn.jointplot(x=x_column_name, y=y_column_name, data=data, kind="reg")
    plot_grid.savefig(str(plot_path))
elif mode in ['regression', 'ci']:
    ci = None
    if mode == 'ci':
        ci = 95
    axes = seaborn.regplot(x=x_column_name, y=y_column_name, data=data, ci=ci)
    fig = axes.get_figure()
    fig.savefig(str(plot_path))
elif mode == 'lineplot':
    axes = seaborn.lineplot(x=x_column_name, y=y_column_name, data=data, hue=group_column_name)
    fig = axes.get_figure()
    fig.savefig(str(plot_path))
else:
    axes = seaborn.scatterplot(x=x_column_name, y=y_column_name, data=data, hue=group_column_name)
    fig = axes.get_figure()
    fig.savefig(str(plot_path))

# Upload output files
worker.upload_output_file(plot_path)

# Save outputs
outputs = {
    "plot": worker.create_output_bitmap(plot_path.name),
}
worker.finish(outputs)

The plot is stored as an output of type Bitmap.

Chart Report

Also consider using chart reports in XF Desktop for interactive plots.

See also Using R in a task type / Using MATLAB in a task type.