I am working with KNIME and trying to create a stacked bar chart that shows the distribution of males and females across different departments in my company.
Here’s a snapshot of my DataFrame after applying a GroupBy node:
insert screenshot here
I would like to transform this data into a stacked bar chart where each bar represents a department, and the bar is split into two sections (one for males, one for females) based on the count of each gender in the department.
However, I am not sure how to achieve this in KNIME. I cannot find suitable options in the “Bar Chart” node. Could someone provide guidance on how to accomplish this?
Hello @Arkhetype and welcome to KNIME forum
You are almost there as there’s is just a question of data shape; You will have to pivot your $Sexe$ column and feed the chart with two column value.
And configure your Bar Chart node as follow. Avg. and Sum will perform identical output in this case (one value per row/service). And you have to select ‘Stacked’ option in ‘General Plot Options’ tab.
In the case you want to represent full 100 stacked percent bars, the configuration would be exactly the same but, you will have to compute the percent values per row/service.
I hope this helps you to achieve the desired result.
i’m using python view node to represent the graph plot. The code is given below.
import knime.scripting.io as knio
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = knio.input_tables.to_pandas()
categories = df[‘Service’].values
female_data = df[‘F’].values
male_data = df[‘M’].values
bar_width = 0.35
index = range(len(categories))
plt.bar(index, female_data, bar_width, label=‘Female’, color=‘lightgreen’)
plt.bar([i + bar_width for i in index], male_data, bar_width, label=‘Male’, color=‘lightgrey’)
plt.title(‘Service Count by Gender in Company XYZ’)
plt.xticks([i + bar_width/2 for i in index], categories, rotation=90) # Adjust the position of x-tick labels
Adding values inside the bars
for i, v in enumerate(female_data):
plt.text(i, v + 1, str(v), color=‘black’, ha=‘center’)
for i, v in enumerate(male_data):
plt.text(i + bar_width, v + 1, str(v), color=‘black’, ha=‘center’)
knio.output_view = knio.view(fig) # alternative: knio.view_matplotlib()
newbie in Knime 5.1
Linux Mint 21.2
Thank you gonhaddock for the response, it was perfect!!! I was still struggling to understand what Knime was expecting as the data type.
And marzoukim, I know it was possible to do it in Python or R, but I preferred to do it in pure Knime.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.