55
66rcParams ['xtick.direction' ] = 'in'
77rcParams ['ytick.direction' ] = 'in'
8-
8+ rcParams [ 'text.usetex' ] = True
99def adjust_spines (ax ,spines ):
1010 ''' Taken from http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html '''
11- for loc , spine in ax .spines .iteritems ():
11+ for loc , spine in ax .spines .items ():
1212 if loc in spines :
13- spine .set_position (('outward' ,10 ))
14- spine .set_smart_bounds (True ) #Doesn't work for log log plots
15- spine .set_linewidth (3 )
13+ spine .set_position (('outward' ,10 )) # outward by 10 points
14+ spine .set_smart_bounds (True )
1615 else :
17- spine .set_color ('none' )
16+ spine .set_color ('none' ) # don't draw spine
17+ # turn off ticks where there is no spine
1818 if 'left' in spines :
1919 ax .yaxis .set_ticks_position ('left' )
2020 else :
21+ # no yaxis ticks
2122 ax .yaxis .set_ticks ([])
2223
2324 if 'bottom' in spines :
2425 ax .xaxis .set_ticks_position ('bottom' )
2526 else :
27+ # no xaxis ticks
2628 ax .xaxis .set_ticks ([])
2729
2830def scree_plot (eigVals ,cutoff = 0.95 ,savename = None , show = False ,ax = None ,cumulative = False ):
@@ -55,7 +57,7 @@ def scree_plot(eigVals,cutoff=0.95,savename=None, show=False,ax=None,cumulative=
5557 plt .show ()
5658 plt .close ()
5759
58- def plot_word_frequency (freqDist ,cutoff = 75 ,normalized = True ,drug = None , poster = False ):
60+ def plot_word_frequency (freqDist ,cutoff = 75 ,normalized = True ,drug = None , poster = False , show = True , savename = None ):
5961 if poster :
6062 rcParams ['axes.linewidth' ]= 2
6163 rcParams ['mathtext.default' ]= 'bf'
@@ -64,24 +66,30 @@ def plot_word_frequency(freqDist,cutoff=75,normalized=True,drug=None, poster = F
6466
6567 labels ,vals = zip (* freqDist .items ())
6668 labels = list (labels )
67- vals = array (vals ).astype (float )
69+ vals = np . array (vals ).astype (float )
6870 vals /= vals .sum () #<--- Normalize values
6971
70- fig = figure (figsize = (6 ,7 ))
71- fig .subplots_adjust (left = 0.08 ,right = 0.98 , top = .95 , bottom = 0.18 )
72+ fig = plt .figure (figsize = (12 ,8 ))
7273 ax = fig .add_subplot (111 )
74+ plt .subplots_adjust (left = 0.08 )
7375
7476 line , = ax .plot (vals [:cutoff ], 'k' , linewidth = 2 )
75- adjust_spines (ax ,['bottom' ,'left' ])
76- ax .set_ylim (ymax = 0.20 )
77+ # adjust_spines(ax,['bottom','left'])
78+ ax .set_ylim (ymax = max ( vals ) + 0.01 )
7779
7880 line .set_clip_on (False )
7981
8082 ax .set_ylabel (r'\Large \textbf{Frequency}' )
81-
82- ax .set_xticks ( arange ( cutoff ) )
83+ ax . set_xticks ( np . arange ( cutoff ))
84+ ax .set_xlim ( 0 , cutoff )
8385 ax .set_xticklabels ([r'\Large \textbf{%s}' % label for label in labels [:cutoff ]], rotation = 90 )
8486
85- ax .annotate (r"\Large \textbf{%s}" % drug .capitalize (), xy = (.5 , .5 ), xycoords = 'axes fraction' ,horizontalalignment = 'center' , verticalalignment = 'center' )
86- tight_layout ()
87+ if drug :
88+ ax .annotate (r"\Large \textbf{%s}" % drug .capitalize (),
89+ xy = (.5 , .5 ), xycoords = 'axes fraction' ,horizontalalignment = 'center' , verticalalignment = 'center' )
8790
91+ if savename :
92+ plt .savefig (savename + '.png' )
93+
94+ if show :
95+ plt .show ()
0 commit comments