Sunday, July 8, 2012

Fun with Python Axes

Sometimes you want to make a plot with two axes, and sometimes you also want the tickmarks on both those axes to look a certain way.  Here's how you do it:




fig = plt.figure(figsize = (11,10))
ax = fig.add_subplot(111)
for i in range(size(magcuts)): ax.plot(xdata[i],ydata[i], label=label[i], linewidth = 2.0)

leg = ax.legend(loc=3,shadow=False,prop=FontProperties(size=25),fancybox = True)
leg.get_frame().set_alpha(0
ax.set_xlabel("$\\chi \\mathrm{\\ (Mpc/h)}$",fontsize=30)
ax.set_ylabel("$f(\\chi)$",fontsize=30)

bx = twiny()
bx.plot(xdata2,ydata,alpha=0)
bx.set_xlabel("$\\mathrm{redshift\\ }(z)$",fontsize=30)

ticklabels = bx.get_xticklabels()
for label in ticklabels:
    label.set_fontsize(18)
    label.set_family('serif')   

ticklabels = ax.get_xticklabels()
for label in ticklabels:
    label.set_fontsize(18)
    label.set_family('serif')            

ticklabels = ax.get_yticklabels()
for label in ticklabels:
    label.set_fontsize(18)
    label.set_family('serif'
    
    
filename = "plot2save"
savefig(filename + '.eps',format = 'eps', transparent=True)
savefig(filename + '.pdf',format = 'pdf', transparent=True)
savefig(filename + '.png',format = 'png', transparent=True)

No comments:

Post a Comment