I have a subplot charted which has the categories that need to be displayed in a specific order. I’m working on the visual using PowerBI Python Script, so it looks like I’m unable to sort the dataframe as PowerBI does it automatically. In my dataset I have fruit_category, rnk, quantity_sold, forecast, goal, and stock quantity. The dataset should be ranked by rnk as it specifies the sort order for the fruit_category. However, I tried doing
dataset = dataset.sort_values('rnk',ascending=True)
but it’s not working. My guess is that it’s PBI not letting me do it. Here’s the graph:
Here’s my code:
import matplotlib.pyplot as plt import numpy as np import seaborn as sns height1 = 0.3 height2 = 0.8 qtysold = dataset.Quantity_Sold forecast = dataset.Forecast goals = dataset.Goal stockqty = dataset.Stock_Quantity fruitcat = dataset.fruit_category sortorder = dataset.rnk numfruitcat = len(fruitcat) dataset = dataset.sort_values('rnk',ascending=True) qgoals = [goals] goal_names = [''] fig,plts = plt.subplots(numfruitcat,1,figsize=(15,7),dpi=500, sharex=True) # size 15x7 fig.subplots_adjust(hspace=0.9) for goalidx in range(0,numfruitcat): idx = len(fruitcat)-1 - goalidx # reverse order because of how subplot numbers them plts[goalidx].barh(fruitcat[idx], goals*0.0025, color='k', height=5, left=goals[idx]) plts[goalidx].barh(fruitcat[idx], forecast[idx], color=(0.627, 0.627, 0.627, 0.3), height=3) plts[goalidx].barh(fruitcat[idx], stockqty[idx], color=(0.682, 0.561, 0.624, 0.2), height=3) plts[goalidx].barh(fruitcat[idx], qtysold[idx], color=(0.851, 0, 0.455,1), height=2) plts[goalidx].text(qtysold[idx],0,'{:,}'.format(qtysold[idx])) plts[goalidx].text(forecast[idx],0,'{:,}'.format(forecast[idx])) plts[goalidx].text(stockqty[idx],0,'{:,}'.format(stockqty[idx])) for count, qtr in enumerate(qgoals): plts[goalidx].text(qtr[idx],3.2,'{}n{:,}'.format(goal_names[count], qtr[idx]),horizontalalignment='left',verticalalignment='top',fontsize=10) plts[goalidx].axis([0, max(stockqty)+100, -1, 1]) plt.legend() sns.despine(right=True) sns.set_theme(style="white") plt.show()
How can I sort my fruits based on the rnk easily? Bonus question, I’m trying to avoid my value labels overlapping. Is there a way to avoid making them overlap? For example, the forecast number and the goal number for Mango is very close, so they are both overlapping.Thanks!!
Advertisement
Answer
You are sorting the dataset after assigning the values, do it before.
dataset = dataset.sort_values('rnk',ascending=True) qtysold = dataset.Quantity_Sold forecast = dataset.Forecast goals = dataset.Goal stockqty = dataset.Stock_Quantity fruitcat = dataset.fruit_category sortorder = dataset.rnk numfruitcat = len(fruitcat)