import knime.scripting.io as knio import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from matplotlib.colors import LinearSegmentedColormap from matplotlib.cm import ScalarMappable import matplotlib matplotlib.use("TkAgg") sns.set() selected_project = knio.flow_variables['value-selection'] #selected_values = knio.flow_variables['value-filter'] data = knio.input_tables[0].to_pandas() df_sorted=data.sort_values(by="Sum(TM Total Cases KM-Exclusive NOT DISTINCT)", ascending=True) # Applica il filtro basato sui valori selezionati df_sorted=data[df_sorted['PROJECT_CODE'] == selected_project] #filtered_data = data[df_sorted['PROJECT_CODE'].isin(selected_values)] # Plot the histogram with a custom color gradient ##fig, ax = plt.subplots() fig, ax = plt.subplots(figsize=(10,10)) sns.set_style('white') # Define a custom colormap from dark blue to dark red #colors = [(0, 0, 0.5), (1, 1, 0), (0.5, 0, 0)] # RGB values for dark blue, yellow, and dark red colors = ['darkred','darkorange'] ##colors = ['blue','red'] #custom_cmap = LinearSegmentedColormap.from_list('custom_gradient', colors, N=256) custom_cmap = LinearSegmentedColormap.from_list('darkred_to_darkorange', colors, N=256) #custom_cmap = LinearSegmentedColormap.from_list('blue_to_red', colors, N=256) # Normalize the data to map it to the custom colormap normalize = plt.Normalize(vmin=df_sorted["Sum(TM Total Cases KM-Exclusive NOT DISTINCT)"].min(), vmax=df_sorted["Sum(TM Total Cases KM-Exclusive NOT DISTINCT)"].max()) bars = plt.barh( y=df_sorted["PROJECT_CODE"], width=df_sorted["Sum(TM Total Cases KM-Exclusive NOT DISTINCT)"], color=custom_cmap(normalize(df_sorted["Sum(TM Total Cases KM-Exclusive NOT DISTINCT)"])), ) plt.title("Case by Project", fontsize=16, fontweight='bold', loc='left') # Set font size, weight, and position plt.grid(False) plt.yticks(df_sorted["PROJECT_CODE"]) # Add each item to the legend #legend_labels = df_sorted["PROJECT_CODE"] #for barh, label in zip(bars, legend_labels): # barh.set_label(label) # Add value annotations over the bars for barh, value in zip(bars, df_sorted["Sum(TM Total Cases KM-Exclusive NOT DISTINCT)"]): #plt.text(barh.get_x() + barh.get_width() + 0.02, barh.get_y + barh.get_height()/2, f'{value}', ha='right', va='center') plt.text(barh.get_x() + barh.get_width() + 1.5, barh.get_y() + barh.get_height() / 2, f'{value}', ha='left', va='center') #plt.legend(loc="best", bbox_to_anchor=(1, 0.5), labelspacing=1.2, borderpad=-8, borderaxespad=0, frameon=False, fontsize=9) #plt.axis('off') # Hide x-axis labels plt.xticks([]) # Add a color scale cbar = plt.colorbar(plt.cm.ScalarMappable(cmap=custom_cmap, norm=normalize), ax=ax, orientation='horizontal', pad=0.02) #cbar.set_label('Your Colorbar Label') # Customize the label as needed # Print the dimensions of the figure in pixels dpi = fig.get_dpi() width_px, height_px = fig.get_size_inches() * dpi print(f"Dimensioni del Grafico: {width_px} x {height_px} pixel") ax.set_facecolor('white') #Controllo zorder #for obj in ax.findobj(): # print(f"Tipo: {type(obj)}, Z-order: {obj.get_zorder()}") # Assign the figure to the output_view variable #knio.output_view = knio.view(fig) knio.output_view = knio.view(plt.gcf())