Feed on
Posts
Comments

Archive for the 'Python' Category

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

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]

Importing arrays from numpy to rpy2 can throw the following error: ValueError: Nothing can be done for the type <type ‘numpy.ndarray’> at the moment. To resolve, add the following to the top of your Python code: import rpy2.robjects.numpy2ri rpy2.robjects.numpy2ri.activate()

to move your x-axis label to the top of the figure: ax.xaxis.set_label_position(‘top’)

to set the size of a figure in matplotlib (in inches): fig.set_size_inches(8,4)

Next »

More blogs about http://desk.stinkpot.org:8080/tricks.