Icon

kn_​example_​python_​graphic_​plot_​two_​different_​y_​axis

KNIME & Python - How to Make a Plot with Two Different Y-axis in Python with Matplotlib

KNIME & Python - How to Make a Plot with Two Different Y-axis in Python with Matplotlib
subfolder /data/ has a Jupyter notebook to try the code "kn_example_python_graphic_plot_two_different_y_axis.ipynb"

adapted from: https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/



KNIME & Python - How to Make a Plot with Two Different Y-axis in Python with Matplotlibsubfolder /data/ has a Jupyter notebook to try the code "kn_example_python_graphic_plot_two_different_y_axis.ipynb"adapted from: https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/https://forum.knime.com/t/is-there-a-way-to-combine-graphs-together/77238/13?u=mlauber71 import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport matplotlibimport matplotlib.pyplot as pltimport pandas as pdimport matplotlib.ticker as tickerinput_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_y_a_variable = knio.flow_variables['variable_y_a']var_y_a_label = knio.flow_variables['label_y_a']var_y_a_colour = knio.flow_variables['colour_y_a']var_y_b_variable = knio.flow_variables['variable_y_b']var_y_b_label = knio.flow_variables['label_y_b']var_y_b_colour = knio.flow_variables['colour_y_b']# create figure and axis objects with subplots()fig,ax = plt.subplots(figsize=(16,9))# make a plotax.plot(input_table[var_x_variable], input_table[var_y_a_variable], color=var_y_a_colour, marker="o")# set x-axis labelax.set_xlabel(var_x_label,fontsize=14)# set y-axis labelax.set_ylabel(var_y_a_label,color=var_y_a_colour,fontsize=14)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')# twin object for two different y-axis on the sample plotax2=ax.twinx()# make a plot with different y-axis using second axis objectax2.plot(input_table[var_x_variable], input_table[var_y_b_variable], color=var_y_b_colour,marker="o")ax2.set_ylabel(var_y_b_label,color=var_y_b_colour,fontsize=14)# plt.show()# 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 Exploring the Power of Python Graphics with KNIME: A Collection of Exampleshttps://medium.com/p/841df87b5563Create an Interactive Dashboard with KNIME Components and Pythonhttps://medium.com/p/41ef794b1467 1.920 x 1.080PNG filefrom_knime_two_different_y_axis_for_single_python_plot_with_twinx.pnggapminder_us.parquetright mouse clickto set the parametersvar_*knio.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.path Image To Table Renderer to Image Table To Image Image Writer (Port) Parquet Reader Python graphics - linechart with two axis Variable toTable Row Python Script KNIME & Python - How to Make a Plot with Two Different Y-axis in Python with Matplotlibsubfolder /data/ has a Jupyter notebook to try the code "kn_example_python_graphic_plot_two_different_y_axis.ipynb"adapted from: https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/https://forum.knime.com/t/is-there-a-way-to-combine-graphs-together/77238/13?u=mlauber71 import knime.scripting.io as knio#Import Libraryfrom io import BytesIOimport osimport matplotlibimport matplotlib.pyplot as pltimport pandas as pdimport matplotlib.ticker as tickerinput_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_y_a_variable = knio.flow_variables['variable_y_a']var_y_a_label = knio.flow_variables['label_y_a']var_y_a_colour = knio.flow_variables['colour_y_a']var_y_b_variable = knio.flow_variables['variable_y_b']var_y_b_label = knio.flow_variables['label_y_b']var_y_b_colour = knio.flow_variables['colour_y_b']# create figure and axis objects with subplots()fig,ax = plt.subplots(figsize=(16,9))# make a plotax.plot(input_table[var_x_variable], input_table[var_y_a_variable], color=var_y_a_colour, marker="o")# set x-axis labelax.set_xlabel(var_x_label,fontsize=14)# set y-axis labelax.set_ylabel(var_y_a_label,color=var_y_a_colour,fontsize=14)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')# twin object for two different y-axis on the sample plotax2=ax.twinx()# make a plot with different y-axis using second axis objectax2.plot(input_table[var_x_variable], input_table[var_y_b_variable], color=var_y_b_colour,marker="o")ax2.set_ylabel(var_y_b_label,color=var_y_b_colour,fontsize=14)# plt.show()# 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 Exploring the Power of Python Graphics with KNIME: A Collection of Exampleshttps://medium.com/p/841df87b5563Create an Interactive Dashboard with KNIME Components and Pythonhttps://medium.com/p/41ef794b1467 1.920 x 1.080PNG filefrom_knime_two_different_y_axis_for_single_python_plot_with_twinx.pnggapminder_us.parquetright mouse clickto set the parametersvar_*knio.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.path Image To Table Renderer to Image Table To Image Image Writer (Port) Parquet Reader Python graphics - linechart with two axis Variable toTable Row Python Script

Nodes

Extensions

Links