Hey guys!
So I’ve started experimenting with coding since around three weeks ago, exclusively using Python and I’ve been somewhat able to Frankenstein a script through online examples.
What I’m trying to make is a Matplotlib graph that will be used in a Python visual in Power BI, placing datasets in specified quadrants based on certain values in Excel.
Right know when I use a slicer in Power BI it places all filtered values at the center of the chart.
Do you guys know of a way, or if it’s possible to make it so it only filters the data, but it doesn’t change their placement in the chart?
Here is my code:
Thank you very much in advance.
So I’ve started experimenting with coding since around three weeks ago, exclusively using Python and I’ve been somewhat able to Frankenstein a script through online examples.
What I’m trying to make is a Matplotlib graph that will be used in a Python visual in Power BI, placing datasets in specified quadrants based on certain values in Excel.
Right know when I use a slicer in Power BI it places all filtered values at the center of the chart.
Do you guys know of a way, or if it’s possible to make it so it only filters the data, but it doesn’t change their placement in the chart?
Here is my code:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: # dataset = pandas.DataFrame(Name, Home Function, GRUE Category , Type of Move) # dataset = dataset.drop_duplicates() # Paste or type your script code here: import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches def quadrant_chart(XXXXX_1, XXXXX_2, Name, TiR, TiS, TiG, xtick_labels=None, ytick_labels=None, ax=None): # Ensure XXXXX_1 and XXXXX_2 have the same length if len(XXXXX_1) != len(XXXXX_2): raise ValueError("XXXXX_1 and XXXXX_2 must have the same length") # Convert 'Yes' to 10 and 'NO' to 1 in both columns XXXXX_1[XXXXX_1 == 'Yes'] = 10 XXXXX_1[XXXXX_1 == 'NO'] = 1 XXXXX_2[XXXXX_2 == 'Yes'] = 10 XXXXX_2[XXXXX_2 == 'NO'] = 1 # make the data easier to work with by putting it in a dataframe data = pd.DataFrame({'XXXXX_1': XXXXX_1, 'XXXXX_2': XXXXX_2, 'Name': Name, 'TiR': TiR, 'TiS': TiS, 'TiG': TiG}) # let the user specify their own axes ax = ax if ax else plt.axes() # calculate averages up front to avoid repeated calculations y_avg = data['XXXXX_2'].mean() x_avg = data['XXXXX_1'].mean() # set x limits adj_x = max((data['XXXXX_1'].max() - x_avg), (x_avg - data['XXXXX_1'].min())) * 1.1 lb_x, ub_x = (x_avg - adj_x, x_avg + adj_x) ax.set_xlim(lb_x, ub_x) # set y limits adj_y = max((data['XXXXX_2'].max() - y_avg), (y_avg - data['XXXXX_2'].min())) * 1.1 lb_y, ub_y = (y_avg - adj_y, y_avg + adj_y) ax.set_ylim(lb_y, ub_y) # set x tick labels if xtick_labels: ax.set_xticks([(x_avg - adj_x / 2), (x_avg + adj_x / 2)]) ax.set_xticklabels(xtick_labels) # Add background color to x tick labels for tick_label in ax.get_xticklabels(): tick_label.set_bbox(dict(facecolor='#FFBB00', alpha=1, edgecolor='none')) # set y tick labels if ytick_labels: ax.set_yticks([(y_avg - adj_y / 2), (y_avg + adj_y / 2)]) ax.set_yticklabels(ytick_labels, color='white', rotation='vertical', va='center') # Add background color to y tick labels for tick_label in ax.get_yticklabels(): tick_label.set_bbox(dict(facecolor='#0e2b63', alpha=1, edgecolor='none')) # plot points and quadrant lines ax.scatter(x=data['XXXXX_1'], y=data['XXXXX_2'], c='lightblue', edgecolor='darkblue', zorder=99) ax.axvline(x_avg, c='k', lw=1) ax.axhline(y_avg, c='k', lw=1) # add data labels for ix, row in data.iterrows(): ax.annotate(row['Name'], (row['XXXXX_1'], row['XXXXX_2']), xytext=(8, -4), textcoords='offset pixels', fontsize=12) # Add 'TiR', 'TiS', and 'TiG' annotations ax.annotate(f"TiR: {row['TiR']}", (row['XXXXX_1'], row['XXXXX_2']), xytext=(50, -15), textcoords='offset pixels', fontsize=8) ax.annotate(f"TiS: {row['TiS']}", (row['XXXXX_1'], row['XXXXX_2']), xytext=(777, -777), textcoords='offset pixels', fontsize=8) ax.annotate(f"TiG: {row['TiG']}", (row['XXXXX_1'], row['XXXXX_2']), xytext=(8, -15), textcoords='offset pixels', fontsize=8) # Show the plot plt.title('TALENT SNAPSHOT') plt.tight_layout(pad=0, w_pad=0, h_pad=0) plt.show() # Assuming the data is already loaded into Power BI and accessible via the DataFrame 'dataset' # Fetch relevant columns from the dataset XXXXX_1 = dataset['XXXXX_1'].to_numpy() XXXXX_2 = dataset['XXXXX_2'].to_numpy() Name = dataset['Name'].to_list() TiR = dataset['TiR'].to_numpy() TiS = dataset['TiS'].to_numpy() TiG = dataset['TiG'].to_numpy() # Call quadrant_chart function with error handling try: quadrant_chart( XXXXX_1=XXXXX_1, XXXXX_2=XXXXX_2, Name=Name, TiR=TiR, TiS=TiS, TiG=TiG, xtick_labels=['PROMOTION', 'LATERAL MOVE'], ytick_labels=['GROW', 'EXPORT'] ) except ValueError as e: print("Error:", e)Sorry if I wasn’t clear or if I said unseeded info, this is my first post and I’m open to feedback. Likewise, I’ll provide more info if needed.
Thank you very much in advance.