Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

Friday, August 13, 2010

Filled step plots in matplotlib

It's not possible to do a simple filled step plot in matplotlib using default
commands. Workaround:


def steppify(arr,isX=False,interval=0):
    """
    Converts an array to double-length for step plotting
    """
    if isX and interval==0:
        interval = abs(arr[1]-arr[0]) / 2.0
        newarr = array(zip(arr-interval,arr+interval)).ravel()
        return newarr

plot(xx,yy,linestyle='steps-mid',color='b',linewidth=1.5)
fill_between(steppify(xx[x1:x2],isX=True),
    steppify(yy[x1:x2])*0,
    steppify(yy[x1:x2]),
    facecolor='b',alpha=0.2)

Tuesday, May 25, 2010

usetex failure in latex documents

When I use matplotlib's internal tex (rcParams['text.useTex']=False), the postscript files generated cause errors that look like this when you try to ps2pdf them:


ps2pdf h2co_pilot.ps
Error: /rangecheck in --get--
Operand stack:
--dict:20/25(ro)(L)-- --nostringval-- 71
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3 %oparray_pop 1739 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- %finish_show --nostringval-- --nostringval-- 9 6 0 --nostringval-- (pdf_text_enum_t) %op_show_continue --nostringval--
Dictionary stack:
--dict:1147/1684(ro)(G)-- --dict:0/20(G)-- --dict:70/200(L)-- --dict:116/300(L)-- --dict:44/200(L)-- --dict:25/42(L)--
Current allocation mode is local
Last OS error: 2
Current file position is 791626
GPL Ghostscript 8.64: Unrecoverable error, exit code 1
make: *** [h2co_pilot.pdf] Error 1


They will not open in MacOS's Preview.app either.

Solution: Make figures with rcParams['text.useTex'] = True

Thursday, February 4, 2010

Logarithmic Colormap / Other Colormap in Matplotlib

This is kind of a pain to find out:


from matplotlib.colors import LogNorm

im = imshow(.... cmap=... , norm=LogNorm(vmin=clevs[0], vmax=clevs[-1]))


It also works for contours, and can be particularly useful if you only want to display contours at a few levels, but you want the colormap to start at a different point. e.g.:

contour(xx,levels=[2,3,4,5,6,7,8,9,10],norm=matplotlib.colors.Normalize(vmin=0,vmax=10))

will start at light blue instead of dark blue in the default colormap