Heloísa Luzzi
Heloísa Luzzi

Reputation: 23

Define title to a matplotlib table

I have a table generated by matplotlib, and I want to insert a title for it. Do anyone know how to do it?

ax = fig.add_subplot(111)
fig = plt.figure(constrained_layout=True)
spec2 = gridspec.GridSpec(ncols=2, nrows=3, figure=fig  )
ax1 = fig.add_subplot(spec2[0, 0])
ax2 = fig.add_subplot(spec2[0, 1])
ax1.axis('off')
ax2.axis('off')

table_data=[
["Ni_Tot ", "NN", "2.5%" , round(df.NN_Ni_Tot.quantile(0.025),3)],
["N. of Samples", count,  "5%" , round(df.NN_Ni_Tot.quantile(0.05),3)],
["Minimum", round(min(df['NN_Ni_Tot'].apply(pd.to_numeric)),3), "25%" , round(df.NN_Ni_Tot.quantile(0.25),3)],
["Maximum", round(max(df['NN_Ni_Tot'].apply(pd.to_numeric)),3), "Median", round(df.NN_Ni_Tot.quantile(0.5),3)],
["Average", round(statistics.mean(df['NN_Ni_Tot'].apply(pd.to_numeric)),3),"75%", round(df.NN_Ni_Tot.quantile(0.75),3)],
["Variance", round(df['NN_Ni_Tot'].var(),2), "95%", round(df.NN_Ni_Tot.quantile(0.95),3)],
["Std Deviation", round(df['NN_Ni_Tot'].std(),2),"97.5%", round(df.NN_Ni_Tot.quantile(0.975),3)],]


table = ax1.table(cellText=table_data, loc='center', cellLoc='center')
table.set_fontsize(14)
table.scale(1.5,1.4)

https://i.sstatic.net/P27Vu.png

Upvotes: 0

Views: 5842

Answers (2)

Heloísa Luzzi
Heloísa Luzzi

Reputation: 23

ax1.set_title("Your title")

The code line above worked, but the title was positioned in the middle of the table, overlapping the data. So I had to ajust it manually

ax1.set_title("NN_NI", fontsize=8, y=1.8, pad=-14)

Upvotes: 1

Teh
Teh

Reputation: 169

ax.set_title("Your title")

Relevant documentation

Upvotes: 3

Related Questions