如何将框和标签完全添加到饼图中,如下图所示

时间:2019-04-23 07:33:12

标签: python matplotlib

尝试创建与下图完全相同的图形,并将其另存为jpeg。 我有一个名为“ custpref”的数据框,如下所示:

tov_type            count
Inpatient               7
Office Visit            6
Appointment Schedule    1
Allergy Sheet           1

尝试的代码如下:-

def addPieGraph():
# Create a list of colors (from iWantHue)
colors = ["#6287da","#72ac5c","#8653aa","#bb7438","#b94b75"]

# Create a pie chart
plt.pie(
    # using data total)arrests
    custpref['cnt'],
    # with the labels being officer names
    labels=custpref['tov_type'],
    # with no shadows
    shadow=False,
    # with colors
    colors=colors,
    # with the start angle at 90%
    startangle=90,
)

# View the plot drop above
plt.axis('equal')

# View the plot
plt.tight_layout()
plt.title("Top 5 Visit Types                  Total = 15 Visits")
plt.savefig(r"PieChart.png",bbox_inches="tight")
plt.show()

预期结果:- expected look of pie graph

上面的代码绘制饼图只需要以下说明:箭头,指向它们的箭头,名称前的计数和矩形框内带有边框的矩形框的标签,如预期输出所示。

(上方数据框和预期图的图例可能会随每个图的变化而变化。我期望我的图看起来与预期图所示完全一样。)

1 个答案:

答案 0 :(得分:0)

注意:虽然这不是您目标的完美再现,但我认为它已经足够接近您调整目标的位置了。

我使用gridspec创建了两个单独的子图(pie_charttitle),添加了自定义注释行(适应了from the documentation),并格式化了{{1 }}子图变成黑色,没有可见的刻度/脊线。

绘制结果:

enter image description here

使用的完整代码:

title