Feed on
Posts
Comments

Archive for the 'Python' Category

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()

hide the axis frame in matplotlib

to hide the axis bordering a figure: fig, ax = pylab.subplots() ax.set_frame_on(False)  

remove axis labels in matplotlib

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)

hide tick lines in matplotlib

to hide the tick lines in matplotlib: xaxis = ax.xaxis xaxis.set_ticks_position(‘none’)

to create only horizontal grid lines in matplotlib: fig, ax = pylab.subplots(1) ax.xaxis.grid(True) pylab.grid()

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)

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.

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))

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’)

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]

« Prev - Next »