I have created a dataframe 'predictions' with columns datetime
, home_team
, home_win
, draw
, away_win
, away_team
datetime
: Date of the match
home_team
: Name of Home Team
home_win
: Home win probability
draw
: Draw Probability
away_win
: Away team win probability
away_team
: Name of away team
Then I plotted the graph mentioned in the image using the code mentioned below:
But I am not able to get two column names on the left y-axis
My desired output is
datetime: Home Team - Home win% - Draw% - Away Team% - Away Team format
I tried using ax.set_yticklabels(predictions['datetime'])
but it only keeps the datetime
and removes the home_team
name.
#Plotting the weekend prediction in Home Team - Home win% - Draw% - Away Team% - Away Team format
#https://stackoverflow.com/questions/34076177/matplotlib-horizontal-bar-chart-barh-is-upside-down
# the simplest solution for this problem is to reverse the pandas dataframe before plotting
predictions = predictions.iloc[::-1]
colors = sns.color_palette("pastel", n_colors=len(predictions['Home_Team']))
customPalette = sns.set_palette(sns.color_palette(colors))
colormap=cmap1,figsize=(10, 6))
ax = predictions.plot.barh(x="Home_Team", y=["home_win", "draw", "away_win"], stacked=True, colormap=customPalette,figsize=(10, 8))
ax2 = ax.twinx() #twinx() is used to have 2 different Y axes with same X axis
predictions.plot.barh(x="Away_Team", y=["home_win", "draw", "away_win"], ax=ax2,stacked=True, colormap=customPalette,figsize=(10, 8))
ax.set_title('%s Weekend Prediction : '%league_url + today.strftime("%d-%b-%Y") + ' to ' + end_date.strftime("%d-%b-%Y"), fontsize = 18)
ax.set_ylabel('Home Team', fontsize = 14)
ax2.set_ylabel('Away Team', fontsize = 14)
#ax.set_yticklabels(y_labels_home_win)
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIGGER_SIZE = 22
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
# move the legend
ax.legend(bbox_to_anchor=(1.25, 1), loc='upper left', borderaxespad=0.)
# Remove top and right borders
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
#https://stackoverflow.com/questions/37039685/hide-axis-values-but-keep-axis-tick-labels-in-matplotlib
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')
for p in ax2.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax2.text(x+width/2,
y+height/2,
'{:.0%}'.format(width),
ha ='center',
va ='center')
ax2.get_legend().remove()
# Turn off tick labels
#ax.set_yticklabels([])
#ax.set_xticklabels([])
#plt.xticks([])
ax.set_xticks([])
ax2.set_xticks([])
Is there a way of doing it?
I can create a new column datetime: home_team
by concatenating datetime
and home_team
columns to get the desired output, but that will disorient the dataframe.
Is there a simpler way of using two columns on a single axis?