Posted in Python on July 6th, 2011 No Comments »
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)
Posted in Python on July 6th, 2011 No Comments »
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)
Posted in Python on July 6th, 2011 No Comments »
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])
Posted in Python on July 2nd, 2011 1 Comment »
to remove the ytick labels in matplotlib: pylab.setp([a.get_yticklabels() for a in fig.axes],visible=False)
Posted in Python on July 2nd, 2011 No Comments »
to hide the yticks in matplotlib: pylab.setp([a.get_yticklines() for a in fig.axes],visible=False)
Posted in Python on June 27th, 2011 No Comments »
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.!
Posted in Python on June 27th, 2011 1 Comment »
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)
Posted in Python on June 14th, 2011 No Comments »
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)
Posted in Python on March 2nd, 2011 No Comments »
to convert a numpy array to a list, use the .tolist() method built into the array class: my_list = my_array.tolist()
Posted in Python on February 24th, 2011 No Comments »
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', [...]