I'm trying to make a polar chart with matplotlib and python 2.7, but I'm struggling on how to increase the space between the X-Axis and the Tick Labels for that same axis. As you can see on the picture, the labels for 12:00 and 6:00 looks just fine, I wish the same space for all other labels.
I tried with
ax.xaxis.LABELPAD = 10
but it doesn't have any effect.
Here is my code (sorry for the mess....):
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
import matplotlib.cm as cm
import matplotlib.ticker as tkr
import pdb
def plot_clock(data,filename,path,**kwargs): # (x,y,colors,lab_x,lab_y,xTicks,filename,legend,**kwargs):
bins = [0,0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12,12.5,13.5,14.5,15.5,16.5,17.5,18.5,19.5,20.5,21.5,22.5,23.5,23.999999];
data = np.array(data)/(60*60)
DATA_ = np.histogram(data,bins)[0]
def hour_formatAM(x, p):
#pdb.set_trace()
if x > 0:
return str(format(x*6/np.pi, "01.0f") + ':00')
else:
return '12:00'
def hour_formatPM(x, p):
#pdb.set_trace()
if x > 0:
return str(format(x*6/np.pi+12, "01.0f") + ':00')
else:
return '24:00'
'''font = {'family' : 'normal',
'weight' : 'bold',
'size' : 12}
mpl.rc('font', **font)'''
mpl.rcParams.update({'font.size': 8})
#sub plot AM
theta = np.array(bins[1:13]) * np.pi / 6
radii = DATA_[1:13]
radii[-1] += DATA_[0]
width = 1 * np.pi / 6
fig = plt.figure(figsize=(5.5,3),dpi=600)
ax = fig.add_subplot(121, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=0)
ax.set_theta_offset(np.pi/2)
ax.set_theta_direction(-1)
ax.xaxis.set_ticks(np.arange(0, np.pi*2, np.pi/6))
ax.get_xaxis().set_major_formatter(tkr.FuncFormatter(hour_formatAM))
ax.yaxis.set_ticks(np.arange(1,max(DATA_),1))
for t, bar in zip(theta, bars):
bar.set_facecolor(plt.cm.jet(t / 12.))
bar.set_alpha(0.5)
#sub plot PM
theta = np.array(bins[14:26]) * np.pi / 6
radii = DATA_[14:26]
radii[-1] += DATA_[13]
width = 1 * np.pi / 6
ax = fig.add_subplot(122, polar=True)
bars = ax.bar(theta, radii, width=width, bottom=0)
ax.set_theta_offset(np.pi/2)
ax.set_theta_direction(-1)
pdb.set_trace()
ax.xaxis.set_ticks(np.arange(0, np.pi*2, np.pi/6))
ax.get_xaxis().set_major_formatter(tkr.FuncFormatter(hour_formatPM))
ax.yaxis.set_ticks(np.arange(1,max(DATA_),1))
for t, bar in zip(theta, bars):
bar.set_facecolor(plt.cm.jet(t / 12.))
bar.set_alpha(0.5)
#pdb.set_trace()
#fig.tight_layout()
#xlabels = [item.get_text() for item in ax.get_xticklabels()]
ax.xaxis.LABELPAD = 10
#[item.set_fontsize(12) for item in ax.xaxis.get_major_ticks()]
fig.subplots_adjust(wspace = 0.4) # http://matplotlib.org/faq/howto_faq.html
fig.savefig(path + filename,format='pdf')
data = [ 10.49531611, 22.49511583, 10.90891806, 18.99525417,
21.57165972, 6.687755 , 6.52137028, 15.86534639,
18.53823556, 6.32563583, 12.99365833, 11.06817056,
17.29261306, 15.31288556, 19.16236667, 10.38483333,
14.51442222, 17.01413611, 6.96102278, 15.98508611,
16.5287 , 15.26533889, 20.83520278, 17.21952056,
7.3225775 , 16.42534361, 14.38649722, 21.63573111, 16.19249444]
data = np.array(data)*60*60
plot_clock(data,'figure2_StartTime.pdf','./')