Pandas Stickers and Title Bar Charts

I am trying to place the x and y axis labels, as well as the heading on a three panel histogram that I created through Pandas, but it seems that I cannot position it correctly. The only result I got in the title and the x-axis label on the very last of the three graphs. I want one common header, xlabel and ylabel. The charting code is below. Any suggestions?

df1.hist(column='human_den',by='region',sharex=True,sharey=True,layout=(1,3)) 
+5
source share
1 answer

Following the accepted answer here , use subplots to create Figure and axis instances of objects.

 import pandas as pd import numpy as np import matplotlib.pyplot as plt # random data df1 = pd.DataFrame(columns=['human_den','region']) df1['human_den'] = np.random.rand(100) df1['region'] = np.random.choice(['Northeast', 'South', 'Midwest'], size=100) # set up figure & axes fig, axes = plt.subplots(nrows=1, ncols=3, sharex=True, sharey=True) # drop sharex, sharey, layout & add ax=axes df1.hist(column='human_den',by='region', ax=axes) # set title and axis labels plt.suptitle('Your Title Here', x=0.5, y=1.05, ha='center', fontsize='xx-large') fig.text(0.5, 0.04, 'common X', ha='center') fig.text(0.04, 0.5, 'common Y', va='center', rotation='vertical') 

Note that the keyword arguments to sharex , sharey and layout not assigned in df1.hist() , in favor of the settings for sharex , sharey , nrows and ncols in plt.subplots to achieve similar effects. An important element is the assignment of the df.hist() argument to the ax keyword for a previously initialized axes object. The name can be set using suptitle .

Generic x and generic common label

+5
source

All Articles