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 matplotlib.pyplot as plt
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']
palette = inputs['palette']
stylesheet = inputs['stylesheet']
custom_style = inputs['custom_style']
mode = inputs['mode']
plt.style.use(stylesheet)
if custom_style is not None:
custom_style_path = worker.create_temp_path("custom_style.rc")
custom_style_path.write_text(custom_style)
matplotlib.rc_file(custom_style_path, use_default_template=False)
if group_column_name is not None and mode not in ('basic', 'lineplot'):
worker.error("Grouping is only supported in basic or lineplot mode.")
# Download input files
table_path = worker.download_input_file(inputs['table'])
plot_path = worker.create_related_path(table_path, '_plot.png')
pdf_path = worker.create_related_path(table_path, '_plot.pdf')
# 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 palette == 'BlackGray':
palette = ['black', 'gray']
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))
plot_grid.savefig(str(pdf_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))
fig.savefig(str(pdf_path))
elif mode == 'lineplot':
axes = seaborn.lineplot(x=x_column_name, y=y_column_name, data=data, hue=group_column_name, palette=palette)
fig = axes.get_figure()
fig.savefig(str(plot_path))
fig.savefig(str(pdf_path))
else:
axes = seaborn.scatterplot(x=x_column_name, y=y_column_name, data=data, hue=group_column_name, palette=palette)
fig = axes.get_figure()
fig.savefig(str(plot_path))
fig.savefig(str(pdf_path))
# Upload output files
worker.upload_output_file(plot_path)
worker.upload_output_file(pdf_path)
# Save outputs
outputs = {
"plot": worker.create_output_bitmap(plot_path.name),
"pdf": worker.create_output_foreign_file(pdf_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.