Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is going on?
#8
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!
Reply


Messages In This Thread
What is going on? - by zimmytheflygirl - Jun-28-2024, 02:59 PM
RE: What is going on? - by deanhystad - Jun-28-2024, 07:03 PM
RE: What is going on? - by zimmytheflygirl - Jun-28-2024, 07:43 PM
RE: What is going on? - by deanhystad - Jun-28-2024, 08:34 PM
RE: What is going on? - by zimmytheflygirl - Jun-29-2024, 12:23 AM
RE: What is going on? - by Pedroski55 - Jun-29-2024, 06:24 AM
RE: What is going on? - by LauraB - Jun-29-2024, 06:31 AM
RE: What is going on? - by Pedroski55 - Jun-29-2024, 07:08 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020