Forum Discussion

jch's avatar
jch
Microsoft Employee
5 years ago
Solved

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

 

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)

 

 

4 Replies

  • Kumail's avatar
    Kumail
    Impactful Individual

    Hello jch 

     

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

     

    This will display the chart like this;

     

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

    • jch's avatar
      jch
      Microsoft Employee

      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

      • Kumail's avatar
        Kumail
        Impactful Individual

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

         

        Hope this helps.

         

        Thanks,

        Kumail Raza