Issue
I am attempting to sort a Bar Plot in Power BI using a Python Script Visual. I would like to sort the Plot by the Count column. The Bar Plot currently has annotated data labels displaying the value from their Count column. Bar Plot Unsorted
When I attempt to sort the dataframe in order to sort the Bar Plot it throws the Data Label annotations out of whack. Bar Plot Data Labels out of Whack
When I run the same code in Jupyter Notebook the Bar Plot is sorted automatically and the data label issue does not occur.
Here is the code I am working from. The first four lines are autogenerated by Power BI's Python Visual.
powerbi: The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
powerbi: dataset = pandas.DataFrame(Name, Count) powerbi: dataset = dataset.drop_duplicates()
powerbi: Paste or type your script code here:
import matplotlib.pyplot as plt
dataset = dataset.sort_values(by=['Count'], ascending=False) #This is my attempt to Sort Bar Plot by Count column which throws the data labels out of whack as illustrated in the second screenshot.
ax = dataset.plot(kind='bar', x='Name', figsize=(15,15), fontsize=14, rot=60)
for idx, label in enumerate(list(dataset.Name)):
value = dataset.Count[idx]
#print(idx,label,value)
ax.annotate(value,
(idx,value))
plt.show()
Here is the dataframe I am working from: DataFrame
How do I sort the bar plot in Power BI by the count column and have the data labels show for the correct category?
Thank you for your help, Tyler
Solution
You need to ignore the index when sorting the dataset
dataset = dataset.sort_values(by=['Count'], ascending=False, ignore_index=True)
otherwise the idx positions of the values will remain the same.
Answered By - Arnau
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.