Posted in Python on March 1st, 2012 No Comments »
the csv module is a helpful module for writing out data in python. here’s how to write out data: import csv …. color_l = ['red','green','blue'] text_l = ['roses','turtles','water'] color_f = open(foo.bar,’w’) header_l=['text','color'] csv_o = csv.DictWriter(color_f,delimiter=’;’,fieldnames=header_l) csv_o.writeheader() row_l = [{'text':a,'color':b} for a,b in zip(text_l,color_l)] csv_o.writerows(row_l) color_f.close()
Posted in Python on February 27th, 2012 No Comments »
to hide the axis bordering a figure: fig, ax = pylab.subplots() ax.set_frame_on(False)
Posted in Python on February 27th, 2012 No Comments »
to remove the labels of the x-axis: ax.get_xaxis().set_ticks([]) to remove the labels of the y-axis: ax.get_yaxis().set_ticks([]) UPDATE (w/ Matplotlib 1.3): An even easier way: pl.tick_params(labeltop=False, labelbottom=False, bottom=False, top=False, labelright=True)
Posted in Python on February 1st, 2012 1 Comment »
to hide the tick lines in matplotlib: xaxis = ax.xaxis xaxis.set_ticks_position(‘none’)
Posted in Python on February 1st, 2012 No Comments »
to create only horizontal grid lines in matplotlib: fig, ax = pylab.subplots(1) ax.xaxis.grid(True) pylab.grid()
Posted in Python on January 31st, 2012 No Comments »
let’s say you’ve made a bar plot and you want to label the bars by strings (e.g. ‘A’, ‘B’) instead of the default number range. to do with a list of labels named ‘bar_l’: import pylab as pl pl.xticks(np.arange(len(bar_l)),bar_l,rotation=45)
Posted in Python on September 28th, 2011 No Comments »
to change the default line color order (color cycle in matplotlib-speak): ax.set_color_cycle(['green', 'orange', 'blue', 'red','brown','pink']) note that this will also go ahead and reset the line counter.
Posted in Python on September 20th, 2011 No Comments »
nan’s cause problems with the fill_between plots in matplotlib. to fix, use the “where” input argument: fig, ax = pl.subplots(1) ax.fill_between(x_v,0,y_v,where=np.isfinite(y_v))
Posted in Python on August 31st, 2011 No Comments »
to only show the bottom ticks in a matplotlib figure: axis_x = ax.xaxis axis_x.tick_bottom() alternatively, you can use the tick_params method: ax.tick_params(‘x’,top=’off’)
Posted in Python on August 8th, 2011 No Comments »
use the itertools and random modules in python to get N pairwise combinations of a list’s items: combo_it = itertools.combinations(my_list,2) combo_l = list(combo_it) random.shuffle(combo_l) pair_l = combo_l[:N]