簡體   English   中英

如何更改堆積條形圖的顏色並添加另一個 y 軸

[英]How to change color of stacked bar chart and add another y axis

所以我創建了一個模擬器,它通過以下方式為我提供了兩場比賽之間的主勝率、平局率和客場勝率的數據(見輸出)

我想以這種格式創建一個堆積條形圖:主隊 - 主隊獲勝% - 平局% - 客隊獲勝% - 客隊

我通過刪除 Away_Team 列並創建堆疊條形圖在一定程度上做到了這一點。 我什至用特定的數據標簽注釋了條形圖。

但我無法:

  1. 更改堆疊條形圖的顏色(如果它的主場是切爾西 vs 曼聯,則主場獲勝百分比為藍色,平局為灰色,客場獲勝百分比為紅色)。但我不知道如何更改顏色的堆積條形圖。

  2. 如何將圖表轉換為主隊 - 主隊獲勝% - 平局% - 客隊獲勝% - 客隊的格式。 我所擁有的是主隊 - 主隊獲勝% - 平局% - 客隊獲勝%

import pandas as pd
import seaborn as sns
import matplotlib as plt
import matplotlib.pyplot as plt

output = pd.read_csv('C:/Python Advanced/python-for-fantasy-football-master/python-for-fantasy-football-master/4 - APIs and JSON Data/Football Leagues Fixtures/EPL/EPL_Prediction.csv')
print(output)
output2 = output.copy()
output2 = output2.drop(['Away_Team'],axis =1)
output2


colors = sns.color_palette("pastel", n_colors=len(output2['Home_Team']))
cmap1 = LinearSegmentedColormap.from_list("my_colormap", colors)

ax = output2.plot(x="Home_Team", y=["home_win", "draw", "away_win"],kind = 'barh', stacked=True, colormap=cmap1,figsize=(10, 6))


ax.set_title('Weekend Prediction')
ax.set_xlabel('Prediction')
ax.set_ylabel('Home Team')
#ax.set_yticklabels(y_labels_home_win)

rects = ax.patches  
    
for p in ax.patches:
    width, height = p.get_width(), p.get_height()
    x, y = p.get_xy() 
    ax.text(x+width/2, 
            y+height/2, 
            '{:.0%}'.format(width), 
            ha ='center', 
            va ='center')    

# move the legend
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) 

Output

                 Home_Team  home_win    draw  away_win             Away_Team
0                    Leeds    0.1986  0.1946    0.6068              West Ham
1  Wolverhampton Wanderers    0.0802  0.1281    0.7917           Aston Villa
2         Newcastle United    0.5892  0.3224    0.0884  West Bromwich Albion
3        Manchester United    0.0894  0.1927    0.7179       Manchester City
4                  Everton    0.1211  0.1502    0.7287               Chelsea
5              Southampton    0.6152  0.2587    0.1261      Sheffield United
6           Crystal Palace    0.1363  0.1678    0.6959             Tottenham
7                   Fulham    0.1010  0.1697    0.7293             Liverpool
8                Leicester    0.2729  0.2401    0.4870              Brighton
9                  Arsenal    0.4552  0.4169    0.1279               Burnley

默認值:主隊 - 主隊勝率% - 平局% - 客隊勝率%

顏色映射:主隊-主場勝率%-平局%-客場勝率%

歡迎來到堆棧溢出。 請注意,這不是代碼編寫或輔導服務。 我們可以幫助解決具體的技術問題,而不是對代碼或建議的開放式請求。 請編輯您的問題以顯示您迄今為止嘗試過的內容,以及您需要幫助的具體問題。 有關如何最好地幫助我們幫助您的詳細信息,請參閱如何提出一個好問題頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM