Wednesday, October 20, 2010

Plotting using Matplotlib

Share axis between two subplots

In order to share the x-axis between two subplot:

import pylab as p
fig = p.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax1)


Plotting the lines before the points
In a plot with several sets of points and lines connecting each set, in order to plot the points on top of the lines (so that the line doesn't show inside the point) use:

R=range(1,10)
P=range(1,10)
p.plot(R,P,color='0.2',lw=1.5, zorder=1)
p.scatter(R,P,s=150,color=c, zorder=2)

Wednesday, October 6, 2010

Encrypt and decrypt files with a password in Linux

There are various ways to make a file password protected. Use one of the following commands to encrypt or decrypt files with a password.

  1. Use GNU gpg command
  2. Use mcrypt command
  3. Use openssl command

gpg Command

To encrypt and decrypt files with a password, use gpg command.

To encrypt single file:
gpg -c filename

This will ask for the pass-phrase and will create filename.gpg.

To decrypt file use gpg command:
gpg filename.gpg

To decrypt a file and write output to file output.txt you can run command:
gpg filename.gpg –o output.txt

Friday, October 1, 2010

Matlab plotting tips

Put two legends in one figure

In matlab it is difficult to put two legends in one figure. However, there are several tricks to get this done. One of them is as follows,


% Plot with 9 curves
ph=plot(rand(20,9));
set(gca,'ylim',[-2,5]);

% Label curves 1 and 5
legend(ph(1),'trace one');

% Add and invisible axes
ah=axes('position',get(gca,'position'),'visible','off');

% Add new legend
legend(ah,ph(5),'trace five','location','west');


Cycle through markers in plot loop

If you are plotting in a loop, and want each loop to use a different plot
marker, then


% Define the markers
markers = '.ox+*sdv^<>ph

% The dataset
y=rand(10);
x=[1:10];

% The Loop for plots
for i=1:10,

% Select marker if there are more data than the markers
marker=markers(rem(i,length(markers))+1)

% Plot
plot(x,y(:,i),marker)
hold on
end



Saturday, April 10, 2010

Debugging Tools - Unix gdb

Main gdb Commands

Before you start, compile the program using the -g option, i.e.

cc -g sourcefile.c

Then to start gdb type

gdb filename
where `filename' is the executable file.

The q (Quit) Command: Quits gdb.

The r (Run) Command: This begins execution program.

The l (List) Command: Use this to list parts of your source file(s). E.g. typing
l 52
will result in display of Line 52 and the few lines following it.

The b (Breakpoint) and c (Continue) Commands: This pause the execution of the program at the specified line. For example,
b 30
stop program every time the program gets to Line 30.

If there is more than one source file, precede the line number by the file name.
To continue executing the program, type c (for the continue command).

One can also use a function name to specify breakpoint. For example,
b main
stop at the first line of the main program.

The d (Display) and p (Print) Commands: This prints value of the indicated variable or expression when the program pauses. E.g.
disp NC
print the value of the variable NC every time the program pauses.
To cancel a display command use the undisplay command.
A related command is p; which prints value of the variable just once.

The printf Command: This is similar to printf in C. E.g., to print two integer variables, X and Y, give gdb the command:
printf "X = %d, Y = %d\n",X,Y

The n (Next) and s (Step) Commands: Execute the next line of the program, and then pause again. If that line happens to be a function call, then n and s will give different results. If you use s, then the next pause will be at the first line of the function; if you use n, then the next pause will be at the line following the function call (the function will be single-step executed, but there will be no pauses within it).

The bt (Backtrace) Command: In case of an execution error with a mysterious message like ``bus error'' or ``segmentation fault,'' the bt command will tell you where in your program this occurred, and if in a function, where the function was called from.

The set Command: Change the value of a program variable. For example, to change value of int variable x in your program, use
set variable x = 12
which will change x's value to 12.

The call Command: Calls a function in your program during execution. E.g.,
call x()

The define Command: Put together one or more commands into a macro. For instance, recall our example from above,
printf "X = %d, Y = %d\n",X,Y

To frequently use this command during your debugging session, you could do:
define pxy
Type commands for definition of "pxy".
End with a line saying just "end".
>printf "%X = %d, Y = %d\n",X,Y
>end

Then you could invoke it just by typing ``pxy''.

The kill (Kill) command:
Stops the program without quitting gdb
kill

You can also use GUI-based interface for a better view of gdb. One of them is
ddd

Tuesday, March 30, 2010

Increase Screen Resolution for Ubuntu in Virtual Box

Virtualbox is one of the many virtualization softwares available online. Virtualization software helps you to try out different Operating system without logging out from your original operating system.

Ubuntu is one of the popular Linux Operating systems available. This article shows how to increase screen resolution of Ubuntu in Virtualbox. (By default, virtualbox only shows two screen resolutions)


1. Start Virtual box and log into Ubuntu.
2. Hit the right ctrl key so you can get your mouse pointer outside the virtual machine.
3. Go to top of virtual window, click on devices then select "Install Guest Additions". You will see a window pop up inside Ubuntu showing you that there are some new files mounted in a virtual CDROM drive. One of those files should be VBoxLinuxAdditions.run
4. Click inside the Ubuntu screen and open a Terminal.
5. Go to directory /media/cdrom0 ( cd /media/cdrom0 )
6. Check files in the directory (Use ls ). You should see amongst the files VBoxLinuxAdditions.run .
7. Run this file. (sudo sh ./VBoxLinuxAdditions.run)
8. Reboot the virtual machine and log onto the Ubuntu. This time go to "System" - "Preferences" and then "Screen Resolution". You should now have more options.

Saturday, March 13, 2010

FreeNX client on Mac

FreeNX client on Mac
I am running FreeNX client on my mac. However, in OSX the ALT key is mapped to Option. When running a X11 application like the FreeNX client, things like ALT-TAB, or ALT-MENU do not work. One way to get around this is to add the following lines to .Xmodmap file.


keycode 66 = Alt_L
clear Mod1
add Mod1 = Alt_L Alt_R


NX Shortcuts
Ctrl + Alt + Shift + Esc to get rid of a not responding session
Ctrl + Alt + F to switch to fullscreen/windowed
Ctrl + Alt + E to toggle the lazy encoding
Ctrl + Alt + J to force a drawable's synchronization
Ctrl + Alt + K enable/disable the catching of Alt+Tab and Print Screen keys

Thursday, February 25, 2010

Vim tips and tricks

Vim search "char" and replace by a line break
In order to replace the newline character by any other character one can use:
:%s/char/\t/gc
As Vim uses '\n' for a newline in patterns, but '\r' for a newline in replacement strings. Check
:help sub-replace-special

Convert Dos files to Unix files
On can remove the carriage return ( ^M ) characters with the following command:
:%s/^M//gc (to delet the carriage returns)
:%s/^M/\t/gc (to replace it by a line break)

To input the ^M character, press Ctrl-v , then press Enter or return.

Easy switch between .h and .c files
While programming in C, one need to alternate between source.c and the corresponding header source.h. Easy to do so by these commands:
:e %<.h
This command opens the corresponding header file.
:sp %<.h
This command opens the corresponding header file in a horizontal split.
:vsp %<.h
This command opens the corresponding header file in a vertical split.

Reformat in vim for a nice column layout
%!column -t

Capitalize words and regions
gu{motion} and gU{motion} commands convert a region to lower/upper case.

Replacing Tabs with spaces and back in VIM
:set noexpandtab
:%retab!


Check file type
:set ft?

Delete all empty lines
:g/^$/d

Delete all empty lines or that contain only whitespace characters
:g/^\s*$/d

Delete all trailing whitespace and condense multiple blank lines into a single blank line
:%s/\s\+$//e
:%s/\n\{3,}/\r\r/e


Avoid the escape key
Use Alt/Meta In a Terminal
:Alt+(h or j or k or l)

Jumping to previously visited locations
Press Ctrl-O to jump back to the previous (older) location.
Press Ctrl-I (same as Tab) to jump forward to the next (newer) location.
:jumps % Display the jump list for the current window


Friday, January 22, 2010

Installing Bengali Fonts in Mac OS X

One can install unicode Bengali font for Mac from:
http://ekushey.org/?page/osx

Detail installing instruction is given here:
http://ekushey.org/?page/osx_font_install_help

The firefox add-on padma along with the unicode front can be used to access sites such as Anandabazar, Bartaman from Mac.