Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jch
Employee
Employee

Python visual returning error

Good afternoon, PBI DT community.

 

I have a Python script (see code below) that is supposed to return the visual listed below but instead it returns an error: Must Python in Power Bi use fields from a Power Bi table to run?

Thanks in advance

jch_0-1626560126828.png

 

import matplotlib.pyplot as plt

#values, colors and labels 
upper = [0.30, 0.60, 3.70, 5.90, 6.28]
titles = ['T1', 'T2', 'T3', 'T4', 'T5']
color_list = ['#243A5E', '#0078D4', '#50E6FF', '#8661C5', '#D59DFF']

# fn
def blockers(fig_w, fig_h, upper, color_list, titles):
    lower = [0] + upper[:-1]
    height = [upper[i] - lower[i] for i in range(len(upper))]
    x_axis_labs = ['{}%: {}'.format(perc, val) for perc, val in zip(upper, titles)]
    # init figure
    plt.figure(figsize=(fig_w, fig_h))
    plt.bar(x=range(len(lower)), height=height, bottom=lower, color=color_list, width=0.5)
    # no borders
    plt.box(False)
    plt.tick_params(left=False, right=False, labelleft=False, labelbottom=False,
                    bottom=False)
    # annotations
    for i in range(len(x_axis_labs)):
        plt.text(
                 x=range(len(x_axis_labs))[i],
                 y=lower[i]-0.15,  # subtract a little more
                 s=x_axis_labs[i],
                 size=10,
                 rotation=270,
                 verticalalignment='center_baseline',
                 )

#call def
blockers(fig_w=1.5, fig_h=1.5, upper=upper, color_list=color_list, titles=titles)

 

 

1 ACCEPTED SOLUTION
Kumail
Post Prodigy
Post Prodigy

Hello @jch 

 

The sample file is given below for your quick reference.

Kumail_1-1626595613651.png

 

https://drive.google.com/file/d/131uz29vhW9uQeIwshgdR1AKoo3XrFTSQ/view?usp=sharing

Regards

Kumail Raza

Did this help? Kudos are appreciated!!

Consider Accept it as the solution to help the other members find it more quickly

View solution in original post

4 REPLIES 4
Kumail
Post Prodigy
Post Prodigy

Hello @jch 

 

The sample file is given below for your quick reference.

Kumail_1-1626595613651.png

 

https://drive.google.com/file/d/131uz29vhW9uQeIwshgdR1AKoo3XrFTSQ/view?usp=sharing

Regards

Kumail Raza

Did this help? Kudos are appreciated!!

Consider Accept it as the solution to help the other members find it more quickly

Kumail
Post Prodigy
Post Prodigy

Hello @jch 

 

Add plt.show() in the blockers function at the end.

 

This will display the chart like this;

Kumail_0-1626593964729.png

 

Regards
Kumail Raza
Did this help? Kudos are appreciated!!
Consider Accept it as the solution to help the other members find it more quickly!!

@Kumail I appreciate your reply. May I have request the one drive file be put on public so I may take a look.

 

I'm having a hard time implementing your solution.

 

Much appreciated

@jch Couldn't find the file upload option here therefore, shared the file through drive.

 

Hope this helps.

 

Thanks,

Kumail Raza

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.