Jun-29-2024, 07:08 AM
(This post was last modified: Jun-30-2024, 07:01 AM by Pedroski55.)
Here is a function for the bar chart exploded! (I am a terrorist!)
def pie_chart(): colours = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'] explode = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1) expense_names = ['beer', 'car', 'computers', 'flowers', 'food', 'fun', 'girlfriend', 'hotels', 'lawyer', 'mobile'] expense_amounts = {name: randint(1, 1001) for name in expense_names} values = list(expense_amounts.values()) names = list(expense_amounts.keys()) total = sum(values) # Wedge properties wp = {'linewidth': 1, 'edgecolor': "blue"} # Creating autocpt arguments # percent values def func(pct, allvalues): absolute = int(pct / 100.*sum(allvalues)) return "{:.1f}%\n(£{:d})".format(pct, absolute) # Creating plot fig, ax = plt.subplots(figsize=(10, 7), num="My Expenses per Week") # setting the axes title invokes the axes don't want to see them so hide them ax.set_title(f"My Expenses Total = £{total}.00", fontsize=16) #hide x-axis ax.get_xaxis().set_visible(False) #hide y-axis ax.get_yaxis().set_visible(False) wedges, texts, autotexts = ax.pie(values, autopct=lambda pct: func(pct, values), explode=explode, labels=names, shadow=True, colors=colours, startangle=90, wedgeprops=wp, textprops=dict(color="black")) # Adding legend ax.legend(wedges, names, title="My Expenses", loc="upper left", bbox_to_anchor=(-0.4, 0, 0.5, 1)) plt.setp(autotexts, size=8, weight="bold") # show plot plt.show()New, improved!