def get_savename(imagefolder, save=False, title=False, ext='png'): """Come up with the savename for the image.""" import os from corpkit.process import urlify # name as if not ext.startswith('.'): ext = '.' + ext if isinstance(save, STRINGTYPE): savename = os.path.join(imagefolder, (urlify(save) + ext)) #this 'else' is redundant now that title is obligatory else: if title: filename = urlify(title) + ext savename = os.path.join(imagefolder, filename) # remove duplicated ext if savename.endswith('%s%s' % (ext, ext)): savename = savename.replace('%s%s' % (ext, ext), ext, 1) return savename
def get_savename(imagefolder, save = False, title = False, ext = 'png'): """Come up with the savename for the image.""" import os from corpkit.process import urlify # name as if not ext.startswith('.'): ext = '.' + ext if isinstance(save, STRINGTYPE): savename = os.path.join(imagefolder, (urlify(save) + ext)) #this 'else' is redundant now that title is obligatory else: if title: filename = urlify(title) + ext savename = os.path.join(imagefolder, filename) # remove duplicated ext if savename.endswith('%s%s' % (ext, ext)): savename = savename.replace('%s%s' % (ext, ext), ext, 1) return savename
def multiplotter(df, leftdict={}, rightdict={}, **kwargs): """ Plot a big chart and its subplots together :param leftdict: a dict of arguments for the big plot :type leftdict: dict :param rightdict: a dict of arguments for the small plot :type rightdict: dict """ from corpkit.interrogation import Interrogation import matplotlib.pyplot as plt axes = [] if isinstance(df, Interrogation): df = df.results df2 = rightdict.pop('data', df.copy()) if isinstance(df2, Interrogation): df2 = df2.results # add more cool layouts here # and figure out a nice way to access them other than numbers... from corpkit.layouts import layouts if isinstance(kwargs.get('layout'), list): layout = kwargs.get('layout') else: lay = kwargs.get('layout', 1) layout = layouts.get(lay) kinda = leftdict.pop('kind', 'area') kindb = rightdict.pop('kind', 'line') tpa = leftdict.pop('transpose', False) tpb = rightdict.pop('transpose', False) numtoplot = leftdict.pop('num_to_plot', len(layout) - 1) ntpb = rightdict.pop('num_to_plot', 'all') sharex = rightdict.pop('sharex', True) sharey = rightdict.pop('sharey', False) if kindb == 'pie': piecol = rightdict.pop('colours', 'default') coloursb = rightdict.pop('colours', 'default') fig = plt.figure() for i, (nrows, ncols, plot_number) in enumerate(layout, start=-1): if tpb: df2 = df2.T if i >= len(df2.columns): continue ax = fig.add_subplot(nrows, ncols, plot_number) if i == -1: df.visualise(kind=kinda, ax=ax, transpose=tpa, num_to_plot=numtoplot, **leftdict) if kinda in ['bar', 'hist', 'barh']: import matplotlib as mpl colmap = [] rects = [ r for r in ax.get_children() if isinstance(r, mpl.patches.Rectangle) ] for r in rects: if r._facecolor not in colmap: colmap.append(r._facecolor) colmap = { list(df.columns)[i]: x for i, x in enumerate(colmap[:len(df.columns)]) } else: colmap = { list(df.columns)[i]: l.get_color() for i, l in enumerate(ax.get_lines()) } else: if colmap and kindb != 'pie': try: name = df2.iloc[:, i].name coloursb = colmap[name] except IndexError: coloursb = 'gray' except KeyError: coloursb = 'gray' if kindb == 'pie': rightdict['colours'] = piecol else: rightdict['colours'] = coloursb df2.iloc[:, i].visualise(kind=kindb, ax=ax, sharex=sharex, sharey=sharey, num_to_plot=ntpb, **rightdict) ax.set_title(df2.iloc[:, i].name) wspace = kwargs.get('wspace', .2) hspace = kwargs.get('hspace', .5) fig.subplots_adjust(bottom=0.025, left=0.025, top=0.975, right=0.975, wspace=wspace, hspace=hspace) size = kwargs.get('figsize', (10, 4)) fig.set_size_inches(size) try: plt.set_size_inches(size) except: pass if kwargs.get('save'): import os from corpkit.process import urlify savepath = os.path.join('images', urlify(kwargs['save']) + '.png') fig.savefig(savepath, dpi=150) return plt
def multiplotter(df, leftdict={},rightdict={}, **kwargs): """ Plot a big chart and its subplots together :param leftdict: a dict of arguments for the big plot :type leftdict: dict :param rightdict: a dict of arguments for the small plot :type rightdict: dict """ from corpkit.interrogation import Interrogation import matplotlib.pyplot as plt axes = [] if isinstance(df, Interrogation): df = df.results df2 = rightdict.pop('data', df.copy()) if isinstance(df2, Interrogation): df2 = df2.results # add more cool layouts here # and figure out a nice way to access them other than numbers... from corpkit.layouts import layouts if isinstance(kwargs.get('layout'), list): layout = kwargs.get('layout') else: lay = kwargs.get('layout', 1) layout = layouts.get(lay) kinda = leftdict.pop('kind', 'area') kindb = rightdict.pop('kind', 'line') tpa = leftdict.pop('transpose', False) tpb = rightdict.pop('transpose', False) numtoplot = leftdict.pop('num_to_plot', len(layout) - 1) ntpb = rightdict.pop('num_to_plot', 'all') sharex = rightdict.pop('sharex', True) sharey = rightdict.pop('sharey', False) if kindb == 'pie': piecol = rightdict.pop('colours', 'default') coloursb = rightdict.pop('colours', 'default') fig = plt.figure() for i, (nrows, ncols, plot_number) in enumerate(layout, start=-1): if tpb: df2 = df2.T if i >= len(df2.columns): continue ax = fig.add_subplot(nrows, ncols, plot_number) if i == -1: df.visualise(kind=kinda, ax=ax, transpose=tpa, num_to_plot=numtoplot, **leftdict) if kinda in ['bar', 'hist', 'barh']: import matplotlib as mpl colmap = [] rects = [r for r in ax.get_children() if isinstance(r, mpl.patches.Rectangle)] for r in rects: if r._facecolor not in colmap: colmap.append(r._facecolor) colmap = {list(df.columns)[i]: x for i, x in enumerate(colmap[:len(df.columns)])} else: colmap = {list(df.columns)[i]: l.get_color() for i, l in enumerate(ax.get_lines())} else: if colmap and kindb != 'pie': try: name = df2.iloc[:, i].name coloursb = colmap[name] except IndexError: coloursb = 'gray' except KeyError: coloursb = 'gray' if kindb == 'pie': rightdict['colours'] = piecol else: rightdict['colours'] = coloursb df2.iloc[:, i].visualise(kind=kindb, ax=ax, sharex=sharex, sharey=sharey, num_to_plot=ntpb, **rightdict) ax.set_title(df2.iloc[:, i].name) wspace = kwargs.get('wspace', .2) hspace = kwargs.get('hspace', .5) fig.subplots_adjust(bottom=0.025, left=0.025, top=0.975, right=0.975, wspace=wspace, hspace=hspace) size = kwargs.get('figsize', (10, 4)) fig.set_size_inches(size) try: plt.set_size_inches(size) except: pass if kwargs.get('save'): import os from corpkit.process import urlify savepath = os.path.join('images', urlify(kwargs['save']) + '.png') fig.savefig(savepath, dpi=150) return plt