我正在使用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)
但我喜欢根据舞台名称指定条形颜色: 例如正在进行中表示黄色,已解决表示绿色, 待定意味着红色。
编辑一个 我通过以下方式解决; (不满意)
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方式 感谢任何更好的答案