matplotlib图例显示问题

时间:2018-03-19 05:55:16

标签: python python-3.x python-2.7 matplotlib spark-dataframe

我从我的数据框中得到两个值.i.e。 ( '通过',失败')。基于这些值尝试构建饼图。我喜欢在图例中显示这两个值,即使我的数据框中没有值。 数据帧值是“通过”,“失败”

colors = ['green','red']

ax_3 = df_spark["Missing_records_check"].value_counts().sort_index(ascending=False).plt(
    kind='pie',
    y="Missing_records_check",
    figsize=(10, 10),
    legend=True,
    autopct='%1.1f%%',
    startangle=90,
    shadow=False,
    colors=['green','red'])

1 个答案:

答案 0 :(得分:0)

创建分类变量会有所帮助。

from pandas.api.types import CategoricalDtype
colors = ['green','red']

labels=['Passed','Failed']

cat_type = CategoricalDtype(categories=labels, ordered=True)
df_spark["Missing_records_check"] = df_spark["Missing_records_check"].astype(cat_type)
ax_3 =df_spark["Missing_records_check"].value_counts().sort_index(ascending=True)\
.plot(kind='pie',y="Missing_records_check", figsize=(10, 10),legend=True,autopct='%1.1f%%',\
     startangle=90,shadow=False,colors=['green','red'])

由于您还想删除不存在的类别的标签,我可能会使用自定义图例。我还添加了一行来根据值对颜色进行排序.-

import pandas as pd
import matplotlib.patches as mpatches

color= ['red' if l == 'Failed' else 'green' for l in df_spark['Missing_records_check']]

ax_3 =df_spark["Missing_records_check"].value_counts().sort_index(ascending=False)\
.plot(kind='pie',y="Missing_records_check", figsize=(10, 10),autopct='%1.1f%%',\
     startangle=90,shadow=False,colors=color, legend=True)

red_patch = mpatches.Patch(color='red', label='Failed')
green_patch = mpatches.Patch(color='green', label='Passed')
ax_3.legend(handles=[green_patch, red_patch])