我目前正在制作一個程序,其中一個酒吧是為客戶服務(wù)的。我希望這個酒吧用不同的顏色來區(qū)分。我曾經(jīng)通過遍歷字典 (barChartObjects) 來做到這一點,當(dāng)字典的鍵與參數(shù)(公司)匹配時,它會改變顏色。發(fā)生這種情況是因為它單獨繪制了每個條形并且運行良好。由于標(biāo)簽的格式問題,我不得不改變我顯示圖形的方式,現(xiàn)在我對如何用我的新功能做我以前所做的事情感到困惑。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以上是我的功能目前的設(shè)置方式。作為參考,下面是我以前使用的函數(shù),它正確地為它們著色,但格式古怪,因此我使用了這個新函數(shù)。
1 回答

慕田峪9158850
TA貢獻1794條經(jīng)驗 獲得超8個贊
這兩種方法看起來都很復(fù)雜。如果我理解正確,您想根據(jù) 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
提交
取消