Icon

kn_​example_​python_​graphic_​histogram_​stacked

KNIME & Python Graphics - Stacked histogram on a log scale with Seaborn

KNIME & Python Graphics - Stacked histogram on a log scale with Seaborn

adapted from: https://seaborn.pydata.org/examples/histogram_stacked.html

KNIME & Python Graphics - Stacked histogram on a log scale with Seabornadapted from: https://seaborn.pydata.org/examples/histogram_stacked.html import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport pandas as pdimport seaborn as snsimport matplotlib as mplimport matplotlib.pyplot as pltsns.set_theme(style="ticks")input_table = knio.input_tables[0].to_pandas()var_title = knio.flow_variables['title_graphic']var_footnote = knio.flow_variables['footnote_graphic']var_x_variable = knio.flow_variables['variable_x']var_x_label = knio.flow_variables['label_x']var_use_log_scale = bool(knio.flow_variables['use_log_scale'])var_x_axis_ticks = knio.flow_variables['x_axis_ticks']# only if ticks are entered in the configurationif var_x_axis_ticks: var_x_axis_ticks2 = list(map(int, var_x_axis_ticks.split(',')))var_y_a_variable = knio.flow_variables['variable_y_a']var_y_a_label = knio.flow_variables['label_y_a']################################################### https://seaborn.pydata.org/examples/scatterplot_sizes.htmlfig, ax = plt.subplots(figsize=(16, 9))sns.despine(fig)sns.histplot( input_table, x=var_x_variable, hue=var_y_a_variable, multiple="stack", palette="light:m_r", edgecolor=".3", linewidth=.5, log_scale=var_use_log_scale,)# only if ticks are entered in the configurationif var_x_axis_ticks: if var_x_axis_ticks2: ax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) ax.set_xticks(var_x_axis_ticks2)# set y-axis labelax.set_xlabel(var_x_label,fontsize=12)# set y-axis label# ax.set_ylabel(var_y_a_label,fontsize=12)# extract the existing legends to add a titlelegend = ax.get_legend()handles = legend.legendHandles# THX D. Paurat for thislabels = [t.get_text() for t in legend.get_texts()]# labels = legend.labels# legend.remove()ax.legend(handles=handles, labels=labels, title=var_y_a_label)ax.annotate(var_footnote, (0,0), (0, -40), xycoords='axes fraction', textcoords='offset points', va='top', size=8)# set a title for the plotfig.suptitle(var_title ,fontsize=18, ha='center')################################################### Create buffer to write intobuffer = BytesIO()# Create plot and write it into the bufferfig.savefig(buffer, format='svg')# The output is the content of the bufferoutput_image = buffer.getvalue()knio.output_images[0] = output_image 1.920 x 1.080PNG filefrom_knime_histogram_stacked.pngdiamonds.parquetknio.flow_variables['var_py_version_pandas'] = pd.__version__knio.flow_variables['var_py_version_numpy'] = np.__version__knio.flow_variables['var_py_version'] = sys.version_infoknio.flow_variables['var_sys_path'] = sys.pathright mouse clickto set the parametersvar_* Image To Table Renderer to Image Table To Image Image Writer (Port) Parquet Reader Python Script Python graphics - Stackedhistogram on a log scale Variable toTable Row KNIME & Python Graphics - Stacked histogram on a log scale with Seabornadapted from: https://seaborn.pydata.org/examples/histogram_stacked.html import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport pandas as pdimport seaborn as snsimport matplotlib as mplimport matplotlib.pyplot as pltsns.set_theme(style="ticks")input_table = knio.input_tables[0].to_pandas()var_title = knio.flow_variables['title_graphic']var_footnote = knio.flow_variables['footnote_graphic']var_x_variable = knio.flow_variables['variable_x']var_x_label = knio.flow_variables['label_x']var_use_log_scale = bool(knio.flow_variables['use_log_scale'])var_x_axis_ticks = knio.flow_variables['x_axis_ticks']# only if ticks are entered in the configurationif var_x_axis_ticks: var_x_axis_ticks2 = list(map(int, var_x_axis_ticks.split(',')))var_y_a_variable = knio.flow_variables['variable_y_a']var_y_a_label = knio.flow_variables['label_y_a']################################################### https://seaborn.pydata.org/examples/scatterplot_sizes.htmlfig, ax = plt.subplots(figsize=(16, 9))sns.despine(fig)sns.histplot( input_table, x=var_x_variable, hue=var_y_a_variable, multiple="stack", palette="light:m_r", edgecolor=".3", linewidth=.5, log_scale=var_use_log_scale,)# only if ticks are entered in the configurationif var_x_axis_ticks: if var_x_axis_ticks2: ax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) ax.set_xticks(var_x_axis_ticks2)# set y-axis labelax.set_xlabel(var_x_label,fontsize=12)# set y-axis label# ax.set_ylabel(var_y_a_label,fontsize=12)# extract the existing legends to add a titlelegend = ax.get_legend()handles = legend.legendHandles# THX D. Paurat for thislabels = [t.get_text() for t in legend.get_texts()]# labels = legend.labels# legend.remove()ax.legend(handles=handles, labels=labels, title=var_y_a_label)ax.annotate(var_footnote, (0,0), (0, -40), xycoords='axes fraction', textcoords='offset points', va='top', size=8)# set a title for the plotfig.suptitle(var_title ,fontsize=18, ha='center')################################################### Create buffer to write intobuffer = BytesIO()# Create plot and write it into the bufferfig.savefig(buffer, format='svg')# The output is the content of the bufferoutput_image = buffer.getvalue()knio.output_images[0] = output_image 1.920 x 1.080PNG filefrom_knime_histogram_stacked.pngdiamonds.parquetknio.flow_variables['var_py_version_pandas'] = pd.__version__knio.flow_variables['var_py_version_numpy'] = np.__version__knio.flow_variables['var_py_version'] = sys.version_infoknio.flow_variables['var_sys_path'] = sys.pathright mouse clickto set the parametersvar_* Image To Table Renderer to Image Table To Image Image Writer (Port) Parquet Reader Python Script Python graphics - Stackedhistogram on a log scale Variable toTable Row

Nodes

Extensions

Links