根据Matplotlib条形图

时间:2016-07-11 07:11:19

标签: python-2.7 pandas matplotlib

我正在使用Matplotlib

制作条形图

下面给出了片段以供参考

import matplotlib
matplotlib.use('Agg')
import datetime
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import pandas as pd 

plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12

current_date = datetime.date.today()
project_task = [{'Project': u'Adding Numbers', 'Stage': u'Resolved'}, {'Project': u'Adding Numbers', 'Stage': u'In Progress'},
                {'Project': u'Adding Numbers', 'Stage': u'In Progress'},{'Project': u'Adding Numbers', 'Stage': u'In Progress'},
                {'Project': u'PMS', 'Stage': u'Resolved'}, {'Project': u'PMS', 'Stage': u'Pending'}, {'Project': u'Adding Numbers', 'Stage': u'In Progress'},
                {'Project': u'Adding Numbers', 'Stage': u'Pending'}, {'Project': u'PMS', 'Stage': u'In Progress'}, {'Project': u'Ajax PMS', 'Stage': u'Resolved'},
                {'Project': u'PMS', 'Stage': u'In Progress'}, {'Project': u'PMS', 'Stage': u'Resolved'}, {'Project': u'PMS', 'Stage': u'In Progress'},
                {'Project': u'PMS', 'Stage': u'In Progress'}]
color_list = ['#007256','#f6c700','#0067a7','#964f8e','#bd1e24','#e97600','#967d46']
data_frame_task = pd.DataFrame(project_task)
if not data_frame_task.empty:
    task_frame = pd.pivot_table(data_frame_task, index='Project', columns='Stage', aggfunc='size', fill_value=0)
    print task_frame
    task = task_frame.plot(kind="bar",stacked=True,width=0.1,color=color)
    plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
    plt.savefig("/tmp/" + str(current_date) + " Task.png", bbox_inches='tight', pad_inches=0, dpi=75)

以上代码以下面的格式生成图形enter image description here

但我喜欢根据舞台名称指定条形颜色: 例如正在进行中表示黄色,已解决表示绿色, 待定意味着红色。

  1. 我的问题是如何在条形图图像中修复该颜色列表。
  2. 如果引入新阶段,则应采用随机颜色。
  3. 编辑一个 我通过以下方式解决; (不满意)

    data_frame_task = pd.DataFrame(project_task)
            if not data_frame_task.empty:
                task_frame = pd.pivot_table(data_frame_task, index='Project', columns='Stage', aggfunc='size', fill_value=0)
                task_list = task_frame.columns.tolist()
                color_list = []
                for task in task_list:
                    if task == "Blocked":
                        color_list.append("#FF0000")
                    elif task == "In Progress":
                        color_list.append('#FFA500')
                    elif task == "Pending":
                        color_list.append("#DB7093")
                    elif task == "Reopen":
                        color_list.append('#0000FF')
                    elif task == "Resolved":
                        color_list.append('#008000')
                    else:
                        color_list.append('#FFFFFF')
                task_frame.plot(kind="bar", stacked=True, width=0.5,color=color_list)
                plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
                plt.savefig("/tmp/" + str(current_date) + " Task.png", bbox_inches='tight', pad_inches=0, dpi=75)
    

    注意这不是pythonic方式   感谢任何更好的答案

0 个答案:

没有答案