Adding plot and axes title to histogram subplots

Question:

I am trying to add individual plot title and axes title. I tried axes.set_xlabel() but getting an error. I used the following code

import matplotlib.pyplot as plt
import pandas as pd

uid_df = pd.read_excel('myexcel.xlsx', sheet_name='UID')
sid_df = pd.read_excel('myexcel.xlsx', sheet_name='SID')


fig, axes = plt.subplots(2, 3)
axes.set_xlabel('x-label')
axes.set_ylabel('y-label')

uid_df.hist('Ass1', ax=axes[0,0], color = "blue", bins = 20)
uid_df.hist('Ass2', ax=axes[0,1], color = "green", bins = 20)
uid_df.hist('FE', ax=axes[0,2], color = "red", bins = 20)
sid_df.hist('Ass1', ax=axes[1,0], color = "blue", bins = 20)
sid_df.hist('Ass2', ax=axes[1,1], color = "green", bins = 20)
sid_df.hist('FE', ax=axes[1,2], color = "red", bins = 20)

plt.show()
Asked By: Tommy

||

Answers:

axes is a 2×3 array if you use plt.subplots(2,3). You have to set the xlabel and ylabel for each element in the array. e.g for the plot in the top left:

axes[0,0].set_xlabel('x-label')
axes[0,0].set_ylabel('y-label')

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html

Answered By: Mack Raymond
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.