Align x tick labels to be left justified - matplotlib - python

I have long tick labels on the x axis, they are rotated, I would like the text to align to the bottom (it would be the left if they were not rotated) I have tried different combinations of ha, and va, but I can not get what I would like - Here is an image of the current plot, and the code:
title = data[0][1]
subtitle = data[0][2]
column_A = 0
column_B = 1
columns = [column_A, column_B]
df = pd.DataFrame(columns=columns)
for sub_data, col in zip(data, columns):
df[col] = sub_data[4:]
# converts str to int
df[column_B] = df[column_B].replace('[\\$,]', '', regex=True).astype(float)
asin_title = df[column_A]
revenue = df[column_B]
# deconstruct fig and ax - this needs to be done before creating the histogram
fig, ax = plt.subplots(figsize=(8, 4), linewidth=1, edgecolor=gray)
# format y ticks, insert comma
# fmtr = StrMethodFormatter(('${x:,g}'))
# ax.yaxis.set_major_formatter(fmtr)
# Removes the spines
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# set the x limit
ax.set_xlim(-1, 30)
# create histogram using asin_title and revenue
plt.bar(asin_title, height=revenue, color=bar_blue,)
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(asin_title)
labels = ['\n'.join(wrap(title[:30] + '..', 22)) for title in asin_title]
ax.set_xticklabels(labels, rotation='vertical', fontsize=6)
for label in ax.xaxis.get_ticklabels()[10:]:
label.set_visible(False)
# for tick in ax.xaxis.get_majorticklabels():
# tick.set_horizontalalignment("left")
# create space from top of chart
plt.subplots_adjust(top=0.13)
# title, subtitle
plt.title(title, fontsize=14, fontweight='bold', ha='left',
color=dark_gray, pad=30, x=-0.04, y=0.98)
# use text() for subtitle with transform=ax.transAxes so matching x offsets line up
plt.text(-0.04, 1.11, subtitle, fontsize=8, fontweight='bold',
ha='left', transform=ax.transAxes, color=gray)
# y ticks
plt.yticks(fontsize=8)
ax.tick_params(axis='y', length=0)
# show lines on the y ticks
plt.grid(b=None, which='major', axis='y')
# set tight_layout
plt.tight_layout()
plt.savefig('top_asins.png')
plt.show()

Related

Seaborn / Matplotlib barplot with multiple y axis

I want to draw a barplot with 3 different y values which belong to RMSE, R2 and MAPE metrics.
My dataframe is;
DLscores = {"GRU":[293.7372606050454,0.961253983114077,86281.57826775634],
"LSTM":[285.9872902525968,0.9632715628933957,81788.73018602304],
"LSTM_Attention":[266.6285102384448,0.9680756432778241,71090.76247197246],
"TCN":[219.30770326715282,0.9784018398981137,48095.868712313546],
"Hybrid":[216.97781461699145,0.978858312741761,47079.372035965505]}
I am able to do this with linegraph. However when I change it to bar, they overlaps. My line plot code is;
# Create figure and axis #1
fig, ax1 = plt.subplots(figsize=(16,10))
# plot line chart on axis #1
p1, = ax1.plot(DLscores.columns, DLscores.iloc[1], color='blue')
ax1.set_ylabel('R2')
#ax1.set_ylim(0, 25)
#ax1.legend(['R2'], loc="upper left")
ax1.yaxis.label.set_color(p1.get_color())
ax1.yaxis.label.set_fontsize(14)
ax1.tick_params(axis='y', colors=p1.get_color(), labelsize=14)
# set up the 2nd axis
ax2 = ax1.twinx()
# plot bar chart on axis #2
p2, = ax2.plot(DLscores.columns, DLscores.iloc[0], color='green')
ax2.grid(False) # turn off grid #2
ax2.set_ylabel('RMSE')
#ax2.set_ylim(0, 90)
#ax2.legend(['RMSE'], loc="upper center")
ax2.yaxis.label.set_color(p2.get_color())
ax2.yaxis.label.set_fontsize(14)
ax2.tick_params(axis='y', colors=p2.get_color(), labelsize=14)
# set up the 3rd axis
ax3 = ax1.twinx()
# Offset the right spine of ax3. The ticks and label have already been placed on the right by twinx above.
ax3.spines.right.set_position(("axes", 1.2))
# Plot line chart on axis #3
p3, = ax3.plot(DLscores.columns, DLscores.iloc[2], color='red')
ax3.grid(False) # turn off grid #3
ax3.set_ylabel('MAPE')
#ax3.set_ylim(0, 8)
#ax3.legend(['MAPE'], loc="upper right")
ax3.yaxis.label.set_color(p3.get_color())
ax3.yaxis.label.set_fontsize(14)
ax3.tick_params(axis='y', colors=p3.get_color(), labelsize=14)
plt.show()
Output:
I also tried seaborn (I couldn't figure it out how can I merge it with "hue"), but code and result is in below:
# plot line chart on axis #1
ax1 = sns.barplot(
x=DLscores.index,
y=DLscores['RMSE'],
color='blue'
)
ax1.set_ylabel('RMSE')
#ax1.set_ylim(0, 8)
ax1.legend(['RMSE'], loc="upper left")
ax1.yaxis.label.set_color('blue')
ax1.yaxis.label.set_fontsize(14)
ax1.tick_params(axis='y', colors='blue', labelsize=14)
# set up the 2nd axis
ax2 = ax1.twinx()
# plot bar chart on axis #2
sns.barplot(
x=DLscores.index,
y=DLscores['R2'],
color='orange',
ax = ax2 # Pre-existing axes for the plot
)
ax2.grid(False) # turn off grid #2
ax2.set_ylabel('R2')
#ax2.set_ylim(0, 90)
ax2.legend(['R2'], loc="upper center")
ax2.yaxis.label.set_color('orange')
ax2.yaxis.label.set_fontsize(14)
ax2.tick_params(axis='y', colors='orange', labelsize=14)
# set up the 3rd axis
ax3 = ax1.twinx()
# Offset the right spine of ax3. The ticks and label have already been placed on the right by twinx above.
ax3.spines.right.set_position(("axes", 1.15))
# Plot line chart on axis #3
p3 = sns.barplot(
x=DLscores.index,
y=DLscores['MAPE'],
color='red',
ax = ax3 # Pre-existing axes for the plot
)
ax3.grid(False) # turn off grid #3
ax3.set_ylabel('MAPE')
#ax3.set_ylim(0, 8)
ax3.legend(['MAPE'], loc="upper right")
ax3.yaxis.label.set_color('red')
ax3.yaxis.label.set_fontsize(14)
ax3.tick_params(axis='y', colors='red', labelsize=14)
plt.show()
I assume the problem is clear.

Problems rotating xtick labels when using twinx

I have problems with the rotation of my X-axis, I have tried to do the rotation the output plot without errors, but I do not have the results.
# Import Data
#df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv")
x = total_test["Dia"].values[:]; y1 = total_test["Confirmados"].values[:]; y2 = total_test["Fallecidos"].values[:]
# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(10,8), dpi= 200)
ax1.plot(x, y1,'g^', color='tab:red')
# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(x, y2,'bs', color='tab:blue')
# Just Decorations!! -------------------
# ax1 (left y axis)
ax1.set_xlabel('Dias', fontsize=10)
ax1.set_ylabel('Personas Confirmadas', color='tab:red', fontsize=20)
ax1.tick_params(axis='y', rotation=0, labelcolor='tab:red' )
# ax2 (right Y axis)
ax2.set_ylabel("Personas Fallecidas", color='tab:blue', fontsize=20)
ax2.tick_params(axis='y', rotation=0, labelcolor='tab:blue')
ax2.set_title("Personas Confirmadas y Fallecidas por Covid-19 Peru", fontsize=15)
#ax2.set_xticks(x)
ax2.set_xticklabels(x[::],fontsize=10,rotation=90)
plt.show()
Any commands for the xaxis need to occur before ax2.
Verify date is in a datetime format and set as the index.
import pandas as pd
import matplotlib.pyplot as plt
# read data
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv")
# verify the date column is a datetime format and set as index
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
#plot
# create figure
fig, ax1 = plt.subplots(1, 1, figsize=(10,8))
# 1st plot
ax1.plot(df['pop'], color='tab:red')
# set xticks rotation before creating ax2
plt.xticks(rotation=90)
# 2nd plot (Right Y Axis)
ax2 = ax1.twinx() # create the 'twin' axis on the right
ax2.plot(df['unemploy'], color='tab:blue')
plt.show()
Plot directly with pandas.DataFrame.plot
# load data
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/economics.csv", parse_dates=True, index_col=[0])
# plot and rotate the tick labels with rot= in the first plot call
ax = df.plot(y='pop', color='tab:red', figsize=(10,8), rot=90)
ax2 = ax.twinx()
df.plot(y='unemploy', color='tab:blue', ax=ax2)
ax2.legend(loc='upper right')

Adding count plot totals and removing specific labels

Hi I have the following code. The code is in a for loop, and it makes over 300 plots.
sns.set(style='white', palette='cubehelix', font='sans-serif')
fig, axs = plt.subplots(2, 3, dpi =200);
fig.subplots_adjust(hspace=0.5, wspace=1)
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False) # labels along the bottom edge are off
#tmppath = 'path/{0}'.format(key);
##
sns.countplot(y='Ethnicity', data=value, orient='h', ax=axs[0,0]);
sns.despine(top=True, right=True, left=True, bottom=True,offset=True)
sns.countplot(y='Program Ratio', data=value,orient='v',ax=axs[1,0]);
sns.despine(offset=True)
sns.countplot(y='Site', data = value, ax=axs[0,1]);
sns.despine(offset=True)
sns.countplot(y='HOUSING_STATUS', data = value, ax = axs[1,1])
sns.despine(offset=True)
sns.countplot(y='Alt. Assessment', data = value, ax = axs[0,2])
sns.despine(offset=True)
pth = os.path.join(tmppath, '{0}'.format(key))
for p in axs.patches:
ax.text(p.get_x() + p.get_width()/2., p.get_width(), '%d' %
int(p.get_width()),
fontsize=12, color='red', ha='center', va='bottom')
#plt.tight_layout(pad=2.0, w_pad=1.0, h_pad=2.0);
plt.set_title('{0}'.format(key)+'Summary')
sns.despine()
axs[0,0].set_xticklabels('','Ethnicity')
axs[1,0].set_axis_labels('','Program Ratio')
axs[0,1].set_axis_labels('','Students by Site')
axs[1,1].set_axis_labels('','Housing Status')
axs[0,2].set_axis_labels('','Alt Assessment')
fig.tight_layout()
fig.subplots_adjust(top=0.88)
fig.suptitle('{0}'.format(key)+' Summary')
plt.suptitle('{0}'.format(key)+' Summary')
plt.savefig("path/{0}/{1}.pdf".format(key,key), bbox_inches = 'tight');
plt.clf()
plt.suptitle('{0} Summary'.format(key))
plt.savefig("path/{0}/{1}.pdf".format(key,key), bbox_inches = 'tight');
plt.clf()
I've checked out the links below ( and more):
Remove xticks in a matplotlib plot?
https://datascience.stackexchange.com/questions/48035/how-to-show-percentage-text-next-to-the-horizontal-bars-in-matplotlib
When I try the method from the second link. I end up with graphs like so
Without that the graph looks something like so
I want to get rid of the words count and the ticks on each subplot xaxis.
#ImportanceOfBeingErnest
Thanks, I followed your advice and this post.
Here is what is a compact version of what I ended up with
sns.set(style='white', palette=sns.palplot(sns.color_palette(ui)), font='sans-serif')
plt.figure(figsize=(20,20))
fig, axs2 = plt.subplots(2, 3, dpi =300);
fig.subplots_adjust(top=.8)
fig.subplots_adjust(hspace=1, wspace=1.5)
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False) # labels along the bottom edge are off
sns.countplot(y='column',palette = ui,order = df.value_counts().index, data=df,
orient='h', ax=axs2[0,0]);
axs2[0,0].set_xlabel('')
axs2[0,0].set_xticks([])
axs2[0,0].set_ylabel('')
axs2[0,0].set_title('label',size = 'small')
axs2[0,0].tick_params(axis='y', which='major', labelsize=8)
sns.despine(top=True, right=True, left=True, bottom=True,offset=True)
for p in axs2[0,0].patches:
axs2[0,0].annotate(int(p.get_width()),((p.get_x() + p.get_width()), p.get_y()), xytext=(15, -10), fontsize=8,color='#000000',textcoords='offset points'
,horizontalalignment='center')
fig.suptitle('{0}#{1}'.format(dur,key)+' Summary', va = 'top', ha= 'center') #size = 'small')
props = dict(boxstyle='square', facecolor='white', alpha=0.5)
fig.text(0.85, 0.925, dt.date.today().strftime("%b %d, %Y"), fontsize=9, verticalalignment='top', bbox=props)
fig.text(0.15, 0.925, 'No. of stuff'+ str(len(value['column'].unique())),fontsize = 10, va = 'top', ha = 'center')
plt.savefig("path/{0}/{1} # {2}.pdf".format(dur,dur,key), bbox_inches = 'tight');
plt.clf()
plt.close('all')
Excuse the black marks, didn't want to show the info

Reverse the order of a legend

I use the following code to plot the bar graph and need to present a legend in reverse order. How can I do it?
colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2)))
p = numpy.empty(len(C2), dtype=object)
plt.figure(figsize=(11, 11))
prevBar = 0
for index in range(len(C2)):
plt.bar(ind, C2[index], width, bottom=prevBar, color=colorsArr[index],
label=C0[index])
prevBar = prevBar + C2[index]
# Positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i + (width/2) for i in ind]
plt.ylabel('Home Category')
plt.title('Affinity - Retail Details(Home category)')
# Set the x ticks with names
plt.xticks(tick_pos, C1)
plt.yticks(np.arange(0, 70000, 3000))
plt.legend(title="Line", loc='upper left')
# Set a buffer around the edge
plt.xlim(-width*2, width*2)
plt.show()
You could call
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Line', loc='upper left')
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
C0 = list('ABCDEF')
C2 = np.random.randint(20000, size=(len(C0), 3))
width = 1.0
C1 = ['foo', 'bar', 'baz']
ind = np.linspace(-width, width, len(C1))
colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2)))
fig = plt.figure(figsize=(11,11))
ax = fig.add_subplot(1, 1, 1)
prevBar = 0
for height, color, label in zip(C2, colorsArr, C0):
h = ax.bar(ind, height, width, bottom=prevBar, color=color, label=label)
prevBar = prevBar + height
plt.ylabel('Home Category')
plt.title('Affinity - Retail Details(Home category)')
# positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i+(width/2.0) for i in ind]
# set the x ticks with names
plt.xticks(tick_pos, C1)
plt.yticks(np.arange(0,70000,3000))
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Line', loc='upper left')
plt.show()
Or you could use the simpler
handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels), title='Line', loc='upper left')
Use a negative number for the legend vertical spacing, like this:
matplotlib.pyplot.stackplot(X, *revDataValues,
linewidth=1.0,
edgecolor='black')
matplotlib.pyplot.legend(revNames,
loc=6, bbox_to_anchor=(1.05, 0.5),
labelspacing=-2.5, frameon=False, # reverse legend
fontsize=9.0)
Stacked Area Chart with reversed legend
I've not tested this as I don't have your data, but this is based on the documentation here on controlling legend entries.
handles = []
for index in range(len(C2)):
h = plt.bar(ind, C2[index], width, bottom=prevBar, color=colorsArr[index], label=C0[index])
handles.append(h)
prevBar = prevBar + C2[index]
plt.legend(title="Line", loc='upper left', handles=handles[::-1])
The newest version of matplotlib (>=3.7) now provides this feature out of the box:
plt.legend(title="Line", loc='upper left', reverse=True)
The default is reverse=False (the previous behaviour)—setting it to reverse=True will now show the entries in the legend in the same order as the stacked bars. See Axes.legend documentation.

Move graph position within plot (matplotlib)

I am trying to create a horizontal bar graph with a table in matplotlib. I am almost there, but fail to align the table with the graph. What I got so far:
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
# Example data
appsol = ['llolLl', 'nnM', 'lllld bbbblnl', 'x2x', 'foobar', 'EXZ', 'Flups', 'Flaps', 'Foobar Barfooment', 'ABC', 'FABAS', 'common', 'AQT', 'Faberjak', 'simsalsa', 'LESS', 'Wermut']
y_pos = np.arange(len(appsol)) - .3
y_pos_2 = np.arange(len(appsol)) - .1
y_pos_3 = np.arange(len(appsol)) + .1
y_pos_4 = np.arange(len(appsol)) + .3
num_tickets = [4, 4,3,2,6,7,8,1,4,4,3,2,6,7,8,1,9]
num_tickets_2 = [7,6,5,4,3,4,2,1,2,4,1,0,3,0,2,1,0]
num_tickets_3 = [1,2,1,1,1,2,2,3,1,1,2,1,3,1,1,2,3]
num_tickets_4 = [8,7,6,2,13,6,8,9,7,6,5,4,3,6,8,9,12]
bar_width = .2
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
# correct yticks
plt.yticks(y_pos_2, appsol)
plt.barh(y_pos, num_tickets, bar_width, align='center', alpha=0.4, color='r')
plt.barh(y_pos_2, num_tickets_2, bar_width, align='center', alpha=0.4, color='b')
plt.barh(y_pos_3, num_tickets_3, bar_width, align='center', alpha=0.4, color='y')
plt.barh(y_pos_4, num_tickets_4, bar_width, align='center', alpha=0.4, color='g')
plt.yticks(y_pos, appsol)
plt.xlabel('Numbers')
plt.title('Horizontal Bar Chart with table')
# Table
empty_labels = ['' for a in appsol ]
plt.yticks(y_pos_2, empty_labels)
plt.tick_params(\
axis='y', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
left='off', # ticks along the bottom edge are off
right='off', # ticks along the top edge are off
labelbottom='off')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.4, bottom=0.2)
cell_text = []
i = len(num_tickets) - 1
for j in num_tickets:
cell_text.append([num_tickets[i], num_tickets_2[i], num_tickets_3[i], num_tickets_4[i]])
i -= 1
row_lables = appsol
column_labels = ['So Huge\nMice', 'Elephants', 'Reptiles', 'Germs']
the_table = ax.table(cellText=cell_text,
rowLabels=row_lables,
colLabels=column_labels,
loc='left')
the_table.set_fontsize(15)
the_table.scale(.5,5.0)
plt.savefig('barh_graph.png')
plt.close('all')
This produces almost what I want, except that the table rows are not aligned with the bars of the graph. So I need a way to either move the graph down half a table row, or the table up half a table row. How can I do this?
I think its easier if you create two subplots and add the table to the left subplot, instead of deriving a new subplot from the single subplot you have now. If you add the table to an existing subplot, you can use the bbox to stretch it from 0 to 1 (so fully) in the y-direction. Since the table has a header, setting the ylim of the right plot to (0, n_items), will make both align properly, and adding a slight offset because the bars are also given an offset of 0.4 (offset of the outer bar + half a barwidth). This should work automatically if the number of elements changes.
bar_width = .2
fig, axs = plt.subplots(1,2, figsize=(12,6))
fig.subplots_adjust(wspace=0, top=1, right=1, left=0, bottom=0)
axs[1].barh(y_pos[::-1], num_tickets[::-1], bar_width, align='center', alpha=0.4, color='r')
axs[1].barh(y_pos_2[::-1], num_tickets_2[::-1], bar_width, align='center', alpha=0.4, color='b')
axs[1].barh(y_pos_3[::-1], num_tickets_3[::-1], bar_width, align='center', alpha=0.4, color='y')
axs[1].barh(y_pos_4[::-1], num_tickets_4[::-1], bar_width, align='center', alpha=0.4, color='g')
axs[1].set_yticks([])
axs[1].set_xlabel('Numbers')
axs[1].set_title('Horizontal Bar Chart with table')
axs[1].set_ylim(0 - .4, (len(appsol)) + .4)
cell_text = list(zip(num_tickets, num_tickets_2, num_tickets_3, num_tickets_4))
row_lables = appsol
column_labels = ['So Huge\nMice', 'Elephants', 'Reptiles', 'Germs']
axs[0].axis('off')
the_table = axs[0].table(cellText=cell_text,
rowLabels=row_lables,
colLabels=column_labels,
bbox=[0.4, 0.0, 0.6, 1.0])
the_table.set_fontsize(15)
plt.savefig('barh_graph.png')
plt.close('all')
If you look closely you might notice that the bars are ranging from the top of the table to the bottom, you could tweak them a bit to make them start at the center of the upper and lower cell of the table.
I found a workaround: I adjust the y-boundaries of the graph:
ax.set_ylim([-.5,17.5])
Of course these numbers only work with these data dimensions.
So still, if someone has a better soltution I would like to see it.
For completness, here is the enhanced version (also changed font size and added a legend):
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
# set font and size
font = {'family' : 'Bitstream Vera Sans',
'size' : 18}
matplotlib.rc('font', **font)
# Example data
appsol = ['llolLl', 'nnM', 'lllld bbbblnl', 'x2x', 'foobar', 'EXZ', 'Flups', 'Flaps', 'Foobar Barfooment', 'ABC', 'FABAS', 'common', 'AQT', 'Faberjak', 'simsalsa', 'LESS', 'Wermut']
y_pos = np.arange(len(appsol)) - .3
y_pos_2 = np.arange(len(appsol)) - .1
y_pos_3 = np.arange(len(appsol)) + .1
y_pos_4 = np.arange(len(appsol)) + .3
num_tickets = [4, 4,3,2,6,7,8,1,4,4,3,2,6,7,8,1,9]
num_tickets_2 = [7,6,5,4,3,4,2,1,2,4,1,0,3,0,2,1,0]
num_tickets_3 = [1,2,1,1,1,2,2,3,1,1,2,1,3,1,1,2,3]
num_tickets_4 = [8,7,6,2,13,6,8,9,7,6,5,4,3,6,8,9,12]
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
# correct yticks
plt.yticks(y_pos_2, appsol)
# this aligns table and graph!!!
ax.set_ylim([-.5,17.5])
labels = ['So Huge Mice', 'Elephants', 'Reptiles', 'Germs']
bar_width = .2
plt.barh(y_pos, num_tickets, bar_width, align='center', alpha=0.4, color='r', label=labels[0])
plt.barh(y_pos_2, num_tickets_2, bar_width, align='center', alpha=0.4, color='b', label=labels[1])
plt.barh(y_pos_3, num_tickets_3, bar_width, align='center', alpha=0.4, color='y', label=labels[2])
plt.barh(y_pos_4, num_tickets_4, bar_width, align='center', alpha=0.4, color='g', label=labels[3])
plt.yticks(y_pos, appsol)
plt.xlabel('Numbers')
plt.title('Horizontal Bar Chart with table')
# Legend
plt.legend(loc='lower center', shadow=True)
num_plots = 4
x_legend = 0.3
y_legend = -0.1
ax.legend(loc='lower center',
bbox_to_anchor=(x_legend, y_legend),
ncol=num_plots, # we want the legend on just one line
shadow=True)
# Table
empty_labels = ['' for a in appsol ]
plt.yticks(y_pos_2, empty_labels)
plt.tick_params(\
axis='y', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
left='off', # ticks along the bottom edge are off
right='off', # ticks along the top edge are off
labelbottom='off')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.4, bottom=0.1)
cell_text = []
i = len(num_tickets) - 1
for j in num_tickets:
cell_text.append([num_tickets[i], num_tickets_2[i], num_tickets_3[i], num_tickets_4[i]])
i -= 1
row_lables = appsol
column_labels = ['So Huge\nMice', 'Elephants', 'Reptiles', 'Germs']
the_table = ax.table(cellText=cell_text,
rowLabels=row_lables,
colLabels=column_labels,
loc='left')
the_table.set_fontsize(18)
the_table.scale(.5,5.34)
plt.savefig('barh_graph.png')
plt.close('all')
And this is what it looks like:

Categories