Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

python - Creating pie chart from dict and changing the arrows

I have some count data in dict like this:

data = {'a_column': 20, 'b_column': 130, 
        'c_column': 140, 'd_column': 300, 'e_column': 150,
        'f_column': 170, 'g_column': 10, 'h_column': 20, 'i_column': 250,'j_column': 54}

I want to plot a pie chart, something like this:

enter image description here

What I have tried:

base_d = sum(list(data.values()))
final_data = {k:m/base_d*100 for k,m in data.items()}
final_data

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 5), subplot_kw=dict(aspect="equal"))
recipe = list(final_data.keys())
data = list(final_data.values())
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
          bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1)/2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(recipe[i], xy=(x, y), xytext=(1*np.sign(x), 1.4*y),
                horizontalalignment=horizontalalignment, **kw)


plt.show()

But that is giving me like this:

enter image description here

How I can make the arrows like expected image and remove the border around the labels?

question from:https://stackoverflow.com/questions/65943049/creating-pie-chart-from-dict-and-changing-the-arrows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I did two thing.

1.Calculate the percentage add to label
2.remove the box style

import numpy as np
import matplotlib.pyplot as plt

def combine_column_names(column_name,cur_value,sums):
    percentage = round(cur_value/sums*100,2)
    name = "{} {}%".format(column_name,percentage)
    return name

data = {'a_column': 20, 'b_column': 130, 
        'c_column': 140, 'd_column': 300, 'e_column': 150,
        'f_column': 170, 'g_column': 10, 'h_column': 20, 'i_column': 250,'j_column': 54}
base_d = sum(list(data.values()))
final_data = {combine_column_names(k,m,base_d):m/base_d*100 for k,m in data.items()}


fig, ax = plt.subplots(figsize=(12, 5), subplot_kw=dict(aspect="equal"))
recipe = list(final_data.keys())
data = list(final_data.values())
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
kw = dict(arrowprops=dict(arrowstyle="-"),zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1)/2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(recipe[i], xy=(x, y), xytext=(1*np.sign(x), 1.4*y),
                horizontalalignment=horizontalalignment, **kw)


plt.show()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...