Friday, September 19, 2014

Multiple Scales in Matplotlib

For my first post, I would like to post an example of multiple scales/axes being used on the same plot in Matplotlib. It also has some example on how manipulate the legend properties. I am going to post many Matplotlib examples here because I want to use them as future references for myself and perhaps others will find them useful.

Here is the code that generates that plot:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from pylab import *
from pylab import log as ln
fig2=figure(2,figsize=(4,3),dpi=96*3)


ax = subplot(111)
zs = arange(0.0, 6.0, 0.01)



xi1 = 0.60
n1 = xi1/(1.0+cosh(zs + ln(2.0*xi1-1.0)))
E1 = -(1.0 - 2.0/(1.0 + exp(-zs - ln(2.0*xi1-1.0))))


ax.plot(append([-1,0,0],zs),append([-1,-1, 1.0-1.0/xi1],E1),linewidth = 2.0,label = r'$\chi = 0.60$',color='b') # I have a piece-wise function t0 plot

axclone = ax.twinx() # where I clone the axes with the same x axis
axclone.plot(zs,n1,linewidth = 2.0,linestyle = '--',label = r'$\chi = 0.60$',color='b')

yt = [0.0, 0.25, 0.5, 0.75]

ytl = ['$0$',
    r'$\frac{1}{4}\frac{N}{\lambda}$',
    r'$\frac{1}{2}\frac{N}{\lambda}$',
    r'$\frac{3}{4}\frac{N}{\lambda}$']

axclone.set_yticks(yt) # setting ticks for the clone axis
axclone.set_yticklabels(ytl)

# Here I very much control the legend properties
leg2 = legend(title = r'$\chi=\frac{\epsilon_1}{\epsilon_1+\epsilon_2}$', fontsize = 14,ncol=2,
    handlelength = 2,
    handleheight = 0.0,
    handletextpad = 0.0,
    borderpad = 0.25,
    borderaxespad = 0.25,
    columnspacing = 0.1,
    loc = 'lower right')
setp(leg2.get_title(),fontsize = 14)#its odd that I had to set the legend title font size with a different function.


axclone.set_ylabel(r'$n\left(z\right)$',fontsize = 22)

#tick positions
E = [-1.0, 1.0-1.0/xi1, -0.5, #0.0, 
    0.5, 1.0]

# tick labels
El = [r'$-1$',
    r'$\left(1-\frac{1}{\chi}\right)$',
    r'$-\frac{1}{2}$', #r'$0$',
    r'$\frac{1}{2}$',
    r'$1$']



# Now I set the axes and move the spines around to look nice
axclone.set_ylim(-0.5,0.5)
ax.set_ylim(-1.1,1.1)
ax.set_xlim(-1,6)
ax.spines['bottom'].set_position('center')
ax.xaxis.set_ticks_position('bottom')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('data',0))
ax.spines['left'].set_smart_bounds(True)


ax.set_yticks(E)
ax.set_yticklabels(El)

ax.set_xticks([-1, #0,    
    1,2,3,4,5,6])
xt = ['$-\lambda$', #'$0$',
    '$\lambda$', 
    '$2\lambda$',
    '$3\lambda$',
    '$4\lambda$', 
    '$5\lambda$', 
    '$6\lambda$']

ax.set_xticklabels(xt)
ax.set_ylabel(r'$\frac{E\left(z\right)}{E_{1,\infty}}$',fontsize = 22)
ax.set_xlabel(r'$z$',fontsize = 22)
fig2.subplots_adjust(bottom=0.00,top=0.95,left=0.17,right=0.85)
savefig('Efield_with_charge_dists.png',fmt='png',dpi=96*3,transparent = True)
show()




No comments:

Post a Comment