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:
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:
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…