Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, February 17, 2012

How to plot a cross shaped axis when using gnuplot

It is always asked by my friends that how to plot a graph with cross shaped axis when using gnuplot. To the best of my knowledge, there is not such a built in axis style in gnuplot. So you have to draw the axis manually. Here a script is provided. You can just use it. If you would like to make some improvements, It is welcomed.

#frame.gnuplot
#Author: 数声风笛离亭晚,我想潇湘君想秦
##########################################################
#Utility: plot a "+" style axis

#Usage:
#call "frame.gnuplot" "ox" "oy" "xmin" "xmax" "ymin"\
#     "ymax" "dx" "dy"

#Parameters: 
#(Frame_ox,Frame_oy):     The position of the origin
#(Frame_xmin,Frame_ymin): The bottum-left point
#(Frame_xmax,Frame_ymax): The top-right point
#Frame_dx:                The interval of x-ticks
#Frame_dy:                The interval of y-ticks

#Before calling this script, Frame_xtic_len and 
#Frame_ytic_len can be set to control the length
#of the corresponding tick length. The length is 
#measured in graph coordinate. The default values 
#are 0.02 and 0.01.

#Example:
#Frame_xtic_len = 0.01
#Frame_ytic_len = 0.01
#call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
#     "2." "2." "0.2"
#plot sin(x)+1.
##########################################################
Frame_ox=$0
Frame_oy=$1
Frame_xmin=$2
Frame_xmax=$3
Frame_ymin=$4
Frame_ymax=$5
Frame_dx=$6
Frame_dy=$7

#set the default value of Frame_x(y)tic_len
if (!exist("Frame_xtic_len")) Frame_xtic_len=0.02
if (!exist("Frame_ytic_len")) Frame_ytic_len=0.01
#no plot of the default border and tics
set border 0
unset tics
#set the xrange and yrange to the user defined value
set xrange [Frame_xmin:Frame_xmax]
set yrange [Frame_ymin:Frame_ymax]
#draw the axis
set arrow from graph 0, first Frame_oy to graph 1.03,\
    first Frame_oy size graph 0.02,15,60 fill ls -1
set arrow from first Frame_ox, graph 0 to first Frame_ox,\
    graph 1.03 size graph 0.015,15,60 fill ls -1
#draw the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
    arrow from Frame_xmin+i*Frame_dx,Frame_oy to \
    Frame_xmin+i*Frame_dx,Frame_oy+ \
    (Frame_ymax - Frame_ymin)*Frame_xtic_len nohead
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
    arrow from Frame_ox,Frame_ymin+i*Frame_dy to \
    Frame_ox+(Frame_xmax - Frame_xmin)*Frame_ytic_len, \
    Frame_ymin+i*Frame_dy nohead
#draw the labels with the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
    label sprintf("%3.1f",Frame_xmin+i*Frame_dx) at \
    Frame_xmin+i*Frame_dx,Frame_oy center offset 0,-0.75
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
    label sprintf("%3.1f",Frame_ymin+i*Frame_dy) at \
    Frame_ox,Frame_ymin+i*Frame_dy right offset -0.75,0

Now let us show how this script is used by an example.

Frame_xtic_len = 0.01
Frame_ytic_len = 0.01
call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
     "2." "2." "0.2"
plot sin(x)+1. notitle

Fig.1 Cross shaped axis plotted by gnuplot


Monday, October 3, 2011

Round corner key box in gnuplot

In a previous post I talked about create a round corner rectangle in gnuplot. But none application was involved in that post. Today I use the method to create a round corner key box.
###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
     -ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
     a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
     -ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
     ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
     a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
     ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
reset
set term png font "Times,18"    #terminal and output file
set output "round_corner_rectangle_key_box.png"
set tics out nomirror
unset key    #key will be created manually
set sample 1000    #samples
#Setting the back ground color
set object 1 rect from graph 0,0 to graph 1,1 back
set object 1 rect fc rgb "#AAAAFF" fillstyle solid 1.0
#The text of the key (some people call it legend)
set label center "y=f(x)" at 5.75,0.7 front
#x and y label
set xlabel "x"
set ylabel "y=f(x)"
#Plot the curve,round corner rectangle and sample line of key
plot sin(5.*x)*exp(-x*x/20.) w l lw 2 lc rgb"green",\
     '+' u 1:(f_low(3.5,0.5,9,0.9,0.5,0.05,$1)):\
     (f_up(3.5,0.5,9,0.9,0.5,0.05,$1)) w filledcurve\
     lc rgb"pink" notitle,\
     x>7.5 && x<8.5 ?0.7:1/0 w l lw 2 lc rgb"green"
Nothing new in this script. The only important and difficult thing is deciding the position of the key text, key sample line and key box. At last we get a picture like this one.

Round corner key box in gnuplot




Saturday, October 1, 2011

Round corner rectangle in gnuplot

A simple rectangle always looks too hard while a round corner rectangle looks more comfortable. In this article I will talk about how to create an round corner rectangle in gnuplot.

There is rectangle object in gnuplot, but unfortunately there seems no option to set it a round corner one. So an absolutely new method will be used. I will use plot style "filledcurve" to create an round corner rectangle. Now come to the script.
###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
     -ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
     a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
     -ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
     ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
     a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
     ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
unset border    #no border
unset tics    #no tics
set sample 1000
set xrange [-10:10]
set yrange [-10:10]
set term png
set output "round_corner_rectangle.png"
plot '+' u 1:(f_low(-10,-10,10,10,1,2,$1)):\
     (f_up(-10,-10,10,10,1,2,$1)) w filledcurve\
     lc rgb"pink" notitle
#plot a round corner rectangle 
Load the plotting script, and we get picture file found_corner_rectangle.png like this.
Round corner rectangle created using filledcurve plot style in gunplot

Friday, September 30, 2011

Rotate a picture using gnuplot

Last time I promised rotating a picture using gnuplot will be talked. Today I come to realize my promise.Let us look at the script at first.
#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rotate.gnuplot" angle inputfile outputfile
#parameters:
##angle: the angle to be rotated
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
angle=$0    #angle to be rotated
angle=angle*pi/180    #change degrees to radians
inputfile="$1.png"    #input file
outputfile="$2.png"    #ouput file
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
set lmargin 0    #nO margin
set rmargin 0
set tmargin 0
set bmargin 0
unset tics    #no tics and border line
unset border
#plot and get the size of the rotated picture
plot inputfile binary filetype=png rotate=angle w rgbima notitle
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax]    #set x and y range
set yrange [ymin:ymax]
set term png truecolor size (xmax-xmin),(ymax-ymin)    #set the terminal
set output outputfile
plot inputfile binary filetype=png rotate=angle w rgbima notitle
The script first plot the plot the rotated picture on screen. This plot has two functions, determining the size of the output picture and letting the user have view at the rotated picture. Save the script as "rotate.gnuplot", and then it can be called like this:
gnuplot> call "rotate.gnuplot" 30 input output
At last is one of our finished work.

Input picture file (initial file download from here)

Picture rotated by 30 degree using our script


Tuesday, September 27, 2011

Convert a rgb colorized png picture to a gray one using gnuplot

I will talk about convert a colorized rgb image to a gray one using gnuplot in this article.
Amazing it is to edit pictures with gnuplot. Yes, it is a little amazing, this kind of works should left to picture editors in fact. But I will show you gnuplot can also do it well.
In a rgb image, the color of a point is marked with (r,g,b) where r,g and b can be and always are diffrent values. While for a gray one r,g and b must be the same. So to convert a rgb image to a gray one, we should convert (r,g,b) to (gray,gray,gray). Once we known how to do it, the problem becomes easy. Now we come to write our gnuplot script.
#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rgbtogray.gnuplot" inputfile outputfile
#parameters:
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
inputfile="$0.png"
outputfile="$1.png"
plot inputfile binary filetype=png w rgbimage
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax]
set yrange [ymin:ymax]
#get the size of the picture
gray(r,g,b)=0.299*r + 0.587*g + 0.114*b
#The function which convert rgb color to gray.
#At first I choose the three weighting all to be 0.333,
#but a firend told me it will be better to use these values
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
#########################
#There should not be any margin in the output picture
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
unset key   #The key, border and tics are all do not need
unset border
unset tics
#set terminal and output file name
set term png size (xmax-xmin),(ymax-ymin)
set output outputfile
#Plot with gray color
plot inputfile\
     u (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3)))\
     binary filetype=png w rgbimage
Save the script as "rgbtogray.gnuplot", then it can be called like this:
gnuplot> call "rgbtogray.gnuplot" inputfile outputfile
The following is two groups of our input and ouput files. The initial input files are copied from here and here (Thanks the original picture author!).
 

Input file-1

Output file-1

Input file-2

Output file-2
Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.