我目前正在制作一个程序,其中一个酒吧是为客户服务的。我希望这个酒吧用不同的颜色来区分。我曾经通过遍历字典 (barChartObjects) 来做到这一点,当字典的键与参数(公司)匹配时,它会改变颜色。发生这种情况是因为它单独绘制了每个条形并且运行良好。由于标签的格式问题,我不得不改变我显示图形的方式,现在我对如何用我的新功能做我以前所做的事情感到困惑。def plotCompany(barChartObjects, company): # axis of data x = [] y = [] print("Company: " + company) # date for the file output name dateForOutput = date.today() # append the attributed input to the corresponding axis for key, value in barChartObjects.items(): x.append(key) y.append(value) freq_series = pd.Series.from_array(y) # Plot the figure. plt.figure(figsize=(12, 8)) # this is where I set the color for the graph, I am assuming I will need to change something here ax = freq_series.plot(kind='bar', color= "#36C989") ax.set_title("Total Shareholder Return (" + (date.today()-timedelta(days=30)).strftime("%b %d %Y") + ")") ax.set_xlabel("Companies") ax.set_ylabel("Percent Change") ax.set_xticklabels(x) plt.text(-0.25, 12, "Median: " + str(medianCalc(barChartObjects)) + "%") add_value_labels(ax) # save the file to my directory. plotDirectory = config.getDirectory(company) plt.savefig(plotDirectory, quality = 95) plt.show() return plotDirectory以上是我的功能目前的设置方式。作为参考,下面是我以前使用的函数,它正确地为它们着色,但格式古怪,因此我使用了这个新函数。
1 回答
慕田峪9158850
TA贡献1794条经验 获得超7个赞
这两种方法看起来都很复杂。如果我理解正确,您想根据 x 轴类别绘制带有颜色的分类条形图。举个例子:
import matplotlib.pyplot as plt
companies = ["Company A", "Company B", "Company C", "Company D"]
values = [17, 23, 12, 32]
myclient = "Company B"
colors = ["grey" if company is not myclient else "red" for company in companies]
plt.bar(companies, values, color=colors)
plt.show()
添加回答
举报
0/150
提交
取消