Icon

kn_​example_​python_​graphic_​bar_​3d_​chart

KNIME (4.6) & Python Graphics - 3D Bar Charts - using bundled Python version

KNIME (4.6) & Python Graphics - 3D Bar Charts - using bundled Python version
(no additional Python installation necessary)

adapted from: https://stackoverflow.com/questions/64709077/how-to-make-error-bars-for-a-3d-bar-graph
https://forum.knime.com/t/3d-bar-chart/46151/3?u=mlauber71

KNIME (4.6) & Python Graphics - 3D Bar Charts - using bundled Python version(no additional Python installation necessary)adapted from: https://stackoverflow.com/questions/64709077/how-to-make-error-bars-for-a-3d-bar-graphhttps://forum.knime.com/t/3d-bar-chart/46151/3?u=mlauber71 /data/kn_example_python_graphic_bar_3d_chart_transposed.ipynb import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport pandas as pdimport numpy as npimport matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfrom matplotlib import cmdf = knio.input_tables[0].to_pandas()# ----------------------------v_remove_variables = {'ID_BusinessPartner', \'ID_Person' }x = df.columnsy = "Type of number"x = [e for e in x if e not in v_remove_variables and e not in y]# ----------------------------ylabels = np.array(df[y]).transpose()xlabels = np.array(x)result = np.array(df[x])var_title = "Overview COVID-19 cases Brazil"var_footnote = "from the KNIME forum ... (https://forum.knime.com/t/3d-bar-chart/46151?u=mlauber71)"################################################### https://stackoverflow.com/questions/64709077/how-to-make-error-bars-for-a-3d-bar-graphresult = np.array(result, dtype=np.int64)colors = ['r','b','g','y','b','p']fig=plt.figure(figsize=(16, 9), dpi=300)ax1=fig.add_subplot(111, projection='3d')ax1.set_xlabel('Confirmation Date (yyyy-mm)', labelpad=10)ax1.set_ylabel('', labelpad=10)ax1.set_zlabel('n cases')# xlabels = np.array(['PED', 'DEV', 'HR1','HR2', 'HR3', 'Trigger'])xpos = np.arange(xlabels.shape[0])# ylabels = np.array(['1','2','3','4','5'])ypos = np.arange(ylabels.shape[0])xposM, yposM = np.meshgrid(xpos, ypos, copy=False)zpos=resultzpos = zpos.ravel()dx=0.5dy=0.5dz=zpos# Replace ax1.w_xaxis with ax1.xaxisax1.xaxis.set_ticks(xpos + dx/2.)ax1.xaxis.set_ticklabels(xlabels)# Replace ax1.w_yaxis with ax1.yaxisax1.yaxis.set_ticks(ypos + dy/2.)ax1.yaxis.set_ticklabels(ylabels)ax1.yaxis.set_tick_params(labelsize=7)ax1.xaxis.set_tick_params(labelsize=7)ax1.zaxis.set_tick_params(labelsize=7)# if you prefer the european number styling with dots as thousands eparators:# ax1.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,.0f}".format(x).replace(",",".") ))# do it US style. Comma is the thousand separatorax1.get_zaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,.0f}".format(x) ))values = np.linspace(0.2, 1., xposM.ravel().shape[0])colors = cm.rainbow(values)ax1.bar3d(xposM.ravel(), yposM.ravel(), dz*0, dx, dy, dz, color=colors)# set footnoteax1.annotate(var_footnote, (0,0), (0, -30), xycoords='axes fraction', textcoords='offset points', va='top', size=8)# set a title for the plotfig.suptitle(var_title ,fontsize=14, 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 regexReplace($Column Name$,"[^a-zA-Z0-9 ]","")=> new column nameinsert new header names1.920 x 1.080PNG filefrom_knime_bar_3d_chart.pngM vs F COVID-19 cases.xlsxreplaceChars(column("new column name"),"" ,"_" )covid_data_br.parquetbr_covid_data_transposed.xlsxbr_covid_data_transposed.parquetPython graphics3D Charts String Manipulation Extract Table Spec Insert ColumnHeader Image To Table Renderer to Image Table To Image Image Writer (Port) Excel Reader Column Expressions Parquet Writer Excel Reader Parquet Writer Python Script KNIME (4.6) & Python Graphics - 3D Bar Charts - using bundled Python version(no additional Python installation necessary)adapted from: https://stackoverflow.com/questions/64709077/how-to-make-error-bars-for-a-3d-bar-graphhttps://forum.knime.com/t/3d-bar-chart/46151/3?u=mlauber71 /data/kn_example_python_graphic_bar_3d_chart_transposed.ipynb import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport pandas as pdimport numpy as npimport matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfrom matplotlib import cmdf = knio.input_tables[0].to_pandas()# ----------------------------v_remove_variables = {'ID_BusinessPartner', \'ID_Person' }x = df.columnsy = "Type of number"x = [e for e in x if e not in v_remove_variables and e not in y]# ----------------------------ylabels = np.array(df[y]).transpose()xlabels = np.array(x)result = np.array(df[x])var_title = "Overview COVID-19 cases Brazil"var_footnote = "from the KNIME forum ... (https://forum.knime.com/t/3d-bar-chart/46151?u=mlauber71)"################################################### https://stackoverflow.com/questions/64709077/how-to-make-error-bars-for-a-3d-bar-graphresult = np.array(result, dtype=np.int64)colors = ['r','b','g','y','b','p']fig=plt.figure(figsize=(16, 9), dpi=300)ax1=fig.add_subplot(111, projection='3d')ax1.set_xlabel('Confirmation Date (yyyy-mm)', labelpad=10)ax1.set_ylabel('', labelpad=10)ax1.set_zlabel('n cases')# xlabels = np.array(['PED', 'DEV', 'HR1','HR2', 'HR3', 'Trigger'])xpos = np.arange(xlabels.shape[0])# ylabels = np.array(['1','2','3','4','5'])ypos = np.arange(ylabels.shape[0])xposM, yposM = np.meshgrid(xpos, ypos, copy=False)zpos=resultzpos = zpos.ravel()dx=0.5dy=0.5dz=zpos# Replace ax1.w_xaxis with ax1.xaxisax1.xaxis.set_ticks(xpos + dx/2.)ax1.xaxis.set_ticklabels(xlabels)# Replace ax1.w_yaxis with ax1.yaxisax1.yaxis.set_ticks(ypos + dy/2.)ax1.yaxis.set_ticklabels(ylabels)ax1.yaxis.set_tick_params(labelsize=7)ax1.xaxis.set_tick_params(labelsize=7)ax1.zaxis.set_tick_params(labelsize=7)# if you prefer the european number styling with dots as thousands eparators:# ax1.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,.0f}".format(x).replace(",",".") ))# do it US style. Comma is the thousand separatorax1.get_zaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,.0f}".format(x) ))values = np.linspace(0.2, 1., xposM.ravel().shape[0])colors = cm.rainbow(values)ax1.bar3d(xposM.ravel(), yposM.ravel(), dz*0, dx, dy, dz, color=colors)# set footnoteax1.annotate(var_footnote, (0,0), (0, -30), xycoords='axes fraction', textcoords='offset points', va='top', size=8)# set a title for the plotfig.suptitle(var_title ,fontsize=14, 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 regexReplace($Column Name$,"[^a-zA-Z0-9 ]","")=> new column nameinsert new header names1.920 x 1.080PNG filefrom_knime_bar_3d_chart.pngM vs F COVID-19 cases.xlsxreplaceChars(column("new column name"),"" ,"_" )covid_data_br.parquetbr_covid_data_transposed.xlsxbr_covid_data_transposed.parquetPython graphics3D ChartsString Manipulation Extract Table Spec Insert ColumnHeader Image To Table Renderer to Image Table To Image Image Writer (Port) Excel Reader Column Expressions Parquet Writer Excel Reader Parquet Writer Python Script

Nodes

Extensions

Links