Home > Enterprise >  Editing a pie chart in pyplot
Editing a pie chart in pyplot

Time:03-01

I'm trying to create a pie chart similar to the one below:

Goal Graph

I don't care that much about the colours, however there are a couple things that I am struggling with.

  1. How can I remove the wedge labels for certain wedges, the ones to the left of 'Other'
  2. How can I place the text inside the 'Other' wedge
  3. How do I set the labels so Other is horizontal, and the others are rotated towards the wedges?

Would I need to re-render the chart several times to get the results I want? I've looked at the documentation but I'm still struggling with the implementation of the chart.

Here is my current output: enter image description here

Here is my current code:

exp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1]
    colList = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red',
               'tab:purple', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']
    pct = '%1.0f%%'
    fdict = {
        'size': 20,
        'weight': 'heavy',
        'family': 'serif',


    }

    plt.figure(figsize=(8, 6), dpi=100)
    plt.pie(data, labels=labels, explode=exp, colors=colList, pctdistance=0.8,
            autopct=pct, startangle=142, rotatelabels=True)
    plt.title(title, fontdict=fdict)
    plt.show()

CodePudding user response:

When plotting pie chart, You get three lists of objects: patches, texts, autotexts. In our context it's list of wedges, labels, percentage_labels indexed same as You labels.

You first need to know what is index of the label, that You want to change. You can easily do it with: label_index = labels.index('OTHER')
Based on this index, You can do manipulation with respective object in any of the list.

To remove any label, You simply set its text to "".

Here is example code to make OTHER label horizontal, while others stay vertical. Also adding new text label with text description and setting it's position below percentage label. I'm also removing first two percentage labels from the plot left of OTHER label. I'm using modified matplotlib official pie chart example, but logic is the same for Your more complex situation.

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'OTHER'
sizes = [10, 30, 30, 30]
explode = (0, 0, 0, 0.1)  # only "explode" the 2nd slice (i.e. 'Hogs')

pct = '%1.0f%%'
fdict = {
    'size': 20,
    'weight': 'heavy',
    'family': 'serif',
}

fig1, ax1 = plt.subplots()
startangle = 142
wedges_lst, labels_lst, percentage_labels_lst = ax1.pie(sizes, explode=explode, labels=labels, pctdistance=0.8,
                                                        autopct=pct,
                                                        startangle=startangle, rotatelabels=True)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

# Get label index
label_index = labels.index('OTHER')
# Get the label text object from the list
label = labels_lst[label_index]

# Set rotation to 0 degrees and center text horizontally
label.update({"rotation": 0, "horizontalalignment": "center"})

# Get percentage label
percentage_label = percentage_labels_lst[label_index]
# Get X, Y position of the percentage label
percentage_label_pos = percentage_label.get_position()

# Create new text label and move it under percentage label
new_label = ax1.text(s="Many specialized technologies \nwere reported only once", x=percentage_label_pos[0],
                     y=percentage_label_pos[1] - 0.2, horizontalalignment="center", verticalalignment="center")

# Remove percentage labels for first two labels
for label in percentage_labels_lst[:2]:
    label.update({"text": ""})

plt.show()

This is the result: enter image description here

  • Related