Forum Discussion
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)
Hello jch
The sample file is given below for your quick reference.
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
4 Replies
- KumailImpactful Individual
Hello jch
The sample file is given below for your quick reference.
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