Feed on
Posts
Comments

Archive for the 'Python' Category

to give NaNs their own specific color in a plot, use a masked array.  for example, the code below will color NaNs white: import numpy.ma import pylab as pl masked_X = numpy.ma.array(X,mask=np.isnan(X)) cmap.set_bad(‘w’,1.) fig, ax = pl.subplots(1) im1 = ax.matshow(masked_X)

to define the color range used by matshow, use the vmin and vmax arguments: import pylab as pl fig, ax = pl.subplots(1) im1 = ax.matshow(X,vmin=0,vmax=1)  

to set the range of the x or y axis in a matplotlib figure: import pylab as pl pl.xlim([day_min,day_max]) pl.ylim([day_min,day_max])

to remove the ytick labels in matplotlib: pylab.setp([a.get_yticklabels() for a in fig.axes],visible=False)

hide yticks in matplotlib

to hide the yticks in matplotlib: pylab.setp([a.get_yticklines() for a in fig.axes],visible=False)

to subtract days from a datetime object in python: from datetime import date, timedelta d=date.today()-timedelta(days=days_to_subtract) thanks steve b.!

to change the font size in a matplotlib legend: import pylab import matplotlib.font_manager as fm fig = pylab.figure() prop = fm.FontProperties(size=5) figlegend.legend(handle_l,label_l,prop=prop)

fit matplotlib figure on page

occasionally, long figure labels will cause portions of your pylab or matplotlib figure to hang off the page.  to change the size of the figure on the page, use the subplots_adjust command: import pylab as pl pl.plot() pl.subplots_adjust(left=.17,bottom=.1,right=.7,top=.82)

to convert a numpy array to a list, use the .tolist() method built into the array class: my_list = my_array.tolist()

when debugging or trying to understand someone else’s python code, it can be helpful to know what variables and methods are associated with an object.  to get that information, use the dir() method: (Pdb) dir(pylab.cm.RdBu) ['N', '__call__', '__doc__', '__init__', '__module__', '_gamma', '_i_bad', '_i_over', '_i_under', '_init', '_isinit', '_lut', '_rgba_bad', '_rgba_over', '_rgba_under', '_segmentdata', '_set_extremes', 'from_list', 'is_gray', 'monochrome', [...]

« Prev - Next »

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