Python Forum
I’m trying to create a Power BI Matplotlib quadrant chart and I’m a little stumped.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I’m trying to create a Power BI Matplotlib quadrant chart and I’m a little stumped.
#1
Question 
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:

 # 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.
Reply
#2
In Power BI, when you filter data, it affects the dataset being passed to the Python script, resulting in changes in the plotted graph. However, you can potentially work around this issue by passing all data points to the Python script and implementing the filtering within the script itself based on the filters applied in Power BI.

Here's how you might adjust your script to handle this:
  • Pass all the data points to Python script from Power BI.
    Modify the quadrant_chart function to filter data based on the criteria specified in Power BI filters.
    Ensure that your Power BI filters are set up to interact with the Python script properly.

here is modified 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})

# Apply filtering based on criteria from Power BI filters
# Example: If 'Filter_Column' is a column in your dataset that is filtered in Power BI
# You can apply the filter like this:
# data = data[data['Filter_Column'] == 'Desired_Value']

# let the user specify their own axes
ax = ax if ax else plt.axes()

# rest of your plotting code...
# (unchanged from your original script)

# 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)

i hope it will work for you

Best regard
Danish hafeez | QA Assistant
buran write Mar-05-2024, 07:00 AM:
Please, stop sharing spam link in your signature. This is not advertising platform.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib.pyplot functions create new figures instead of applying to the current one karkas 2 2,131 Jul-09-2020, 08:32 AM
Last Post: karkas
  Rename labels of a bar chart Matplotlib smalatray 1 4,359 Jul-01-2020, 01:48 AM
Last Post: hussainmujtaba
  Prediction of Coal Fire Power Plant Pollutants Emission Dalpi 2 2,170 May-08-2020, 06:28 PM
Last Post: Dalpi
  Matplotlib bar chart ollarch 0 1,406 Mar-04-2020, 10:45 AM
Last Post: ollarch
  How to create matplotlib subplots from figures vitaly 3 3,144 Mar-02-2020, 12:58 AM
Last Post: scidam
  Spacing pie chart colours evenly in matplotlib? Giovanni_diJacopo 1 3,298 Jul-12-2019, 12:31 PM
Last Post: scidam
  how can I create a recursive graphic with matplotlib royer14 3 3,758 Nov-22-2018, 05:00 PM
Last Post: Gribouillis
  how can I create graphics using matplotlib royer14 8 4,205 Nov-21-2018, 07:02 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020