Icon

kn_​example_​python_​graphic_​plot_​large_​number_​two_​y_​axis_​date

KNIME & Python - How to Make a large Plot with Two Different Y-axis in Python with Matplotlib and Date X-Axis

KNIME & Python - How to Make a large Plot with Two Different Y-axis in Python with Matplotlib and Date X-Axis
https://forum.knime.com/t/hybrid-graph/64012/2?u=mlauber71

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 large Plot with Two Different Y-axis in Python with Matplotlib and Date X-Axishttps://forum.knime.com/t/hybrid-graph/64012/2?u=mlauber71adapted from: https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/ import knime.scripting.io as knioimport pandas as pdimport numpy as np#Import Libraryfrom io import BytesIOimport osimport matplotlibimport matplotlib.pyplot as pltfrom matplotlib.ticker import MaxNLocator, FuncFormatterfrom matplotlib.dates import AutoDateLocator, AutoDateFormatterinput_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']# input_table[var_x_variable]# input_table[var_y_a_variable]# input_table[var_y_b_variable]# Define a function to format the tick labels with thousands separatorsdef thousands_formatter(x, pos): return f'{x:,.0f}'# Create a figure and a set of subplotsfig, ax1 = plt.subplots(figsize=(16, 9))# Create the line chart for the first y# Generate sample datadate_rng = input_table[var_x_variable]y1 = input_table[var_y_a_variable]y2 = input_table[var_y_b_variable]# Create a figure and a set of subplotsfig, ax1 = plt.subplots(figsize=(16, 9))var_ax1_color = var_y_a_colour + "-"# Create the line chart for the first y-axisax1.plot(date_rng, y1, var_ax1_color, alpha=0.7, marker="o")ax1.set_xlabel(var_x_label, fontsize=14)ax1.set_ylabel(var_y_a_label, color=var_y_a_colour, fontsize=14)ax1.tick_params('y', colors=var_y_a_colour)ax1.yaxis.set_major_formatter(FuncFormatter(thousands_formatter))# Set the y1-axis tick locatorax1.yaxis.set_major_locator(MaxNLocator(integer=True, prune='both', nbins=10))# Create a second y-axis sharing the same x-axisax2 = ax1.twinx()var_ax2_color = var_y_b_colour + "-"# Create the line chart for the second y-axisax2.plot(date_rng, y2, var_ax2_color, alpha=0.7, marker="o")ax2.set_ylabel(var_y_b_label, color=var_y_b_colour, fontsize=14)ax2.tick_params('y', colors=var_y_b_colour)ax2.yaxis.set_major_formatter(FuncFormatter(thousands_formatter))# Set the y2-axis tick locatorax2.yaxis.set_major_locator(MaxNLocator(integer=True, prune='both', nbins=10))# Set the x-axis tick locator and formatterdate_locator = AutoDateLocator()date_formatter = AutoDateFormatter(date_locator)ax1.xaxis.set_major_locator(date_locator)ax1.xaxis.set_major_formatter(date_formatter)ax1.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_python_graphic_plot_large_number_two_y_axis_date.pngright 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.pathforum data hybrid graph.xlsxNode 2413sort date DESCdf_data.parquet Image To Table Renderer to Image Table To Image Image Writer (Port) Python graphics - linechart with two axis Variable toTable Row Python Script Excel Reader String to Date&Time Sorter String ToNumber (PMML) Parquet Writer KNIME & Python - How to Make a large Plot with Two Different Y-axis in Python with Matplotlib and Date X-Axishttps://forum.knime.com/t/hybrid-graph/64012/2?u=mlauber71adapted from: https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/ import knime.scripting.io as knioimport pandas as pdimport numpy as np#Import Libraryfrom io import BytesIOimport osimport matplotlibimport matplotlib.pyplot as pltfrom matplotlib.ticker import MaxNLocator, FuncFormatterfrom matplotlib.dates import AutoDateLocator, AutoDateFormatterinput_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']# input_table[var_x_variable]# input_table[var_y_a_variable]# input_table[var_y_b_variable]# Define a function to format the tick labels with thousands separatorsdef thousands_formatter(x, pos): return f'{x:,.0f}'# Create a figure and a set of subplotsfig, ax1 = plt.subplots(figsize=(16, 9))# Create the line chart for the first y# Generate sample datadate_rng = input_table[var_x_variable]y1 = input_table[var_y_a_variable]y2 = input_table[var_y_b_variable]# Create a figure and a set of subplotsfig, ax1 = plt.subplots(figsize=(16, 9))var_ax1_color = var_y_a_colour + "-"# Create the line chart for the first y-axisax1.plot(date_rng, y1, var_ax1_color, alpha=0.7, marker="o")ax1.set_xlabel(var_x_label, fontsize=14)ax1.set_ylabel(var_y_a_label, color=var_y_a_colour, fontsize=14)ax1.tick_params('y', colors=var_y_a_colour)ax1.yaxis.set_major_formatter(FuncFormatter(thousands_formatter))# Set the y1-axis tick locatorax1.yaxis.set_major_locator(MaxNLocator(integer=True, prune='both', nbins=10))# Create a second y-axis sharing the same x-axisax2 = ax1.twinx()var_ax2_color = var_y_b_colour + "-"# Create the line chart for the second y-axisax2.plot(date_rng, y2, var_ax2_color, alpha=0.7, marker="o")ax2.set_ylabel(var_y_b_label, color=var_y_b_colour, fontsize=14)ax2.tick_params('y', colors=var_y_b_colour)ax2.yaxis.set_major_formatter(FuncFormatter(thousands_formatter))# Set the y2-axis tick locatorax2.yaxis.set_major_locator(MaxNLocator(integer=True, prune='both', nbins=10))# Set the x-axis tick locator and formatterdate_locator = AutoDateLocator()date_formatter = AutoDateFormatter(date_locator)ax1.xaxis.set_major_locator(date_locator)ax1.xaxis.set_major_formatter(date_formatter)ax1.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_python_graphic_plot_large_number_two_y_axis_date.pngright 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.pathforum data hybrid graph.xlsxNode 2413sort date DESCdf_data.parquet Image To Table Renderer to Image Table To Image Image Writer (Port) Python graphics - linechart with two axis Variable toTable Row Python Script Excel Reader String to Date&Time Sorter String ToNumber (PMML) Parquet Writer

Nodes

Extensions

Links