Saturday, September 17, 2011

Plot histograms using boxes

Some one may ask:"There is histogram plot style in gnuplot, why plot it with boxes?" I would like to say there is some restriction on the built in histogram plot style, for example the x-axis is always using the row number, you can not make it using the coloumns in the data file.

The simplest case is that there is only one group of data to be plotted. In this case you just set the boxwidth to a proper value, for example 0.95, and plot it with boxes. Here is an example.
The data file is like this:
1975    0.5     9.0
1980    2.0     12.0
1985    2.5     10.1
1990    2.6     9.1
1995    2.0     7.2
2000    5.0     8.0
2005    10.2    6.0
2010    15.1    6.2
The plotting script is like this:
reset
set term png truecolor
set output "profit.png"
set xlabel "Year"
set ylabel "Profit(Million Dollars)"
set grid
set boxwidth 0.95 relative
set style fill transparent solid 0.5 noborder
plot "profit.dat" u 1:2 w boxes lc rgb"green" notitle
This example plot a graph like this one:

Plot histogram using boxes with one group of data

When there is more than one group of data to plot, the boxwidth and gap between the boxes should be calculated carefully. We do it like this:
reset
dx=5.
n=2
total_box_width_relative=0.75
gap_width_relative=0.1
d_width=(gap_width_relative+total_box_width_relative)*dx/2.
reset
set term png truecolor
set output "profit.png"
set xlabel "Year"
set ylabel "Profit(Million Dollars)"
set grid
set boxwidth total_box_width_relative/n relative
set style fill transparent solid 0.5 noborder
plot "profit.dat" u 1:2 w boxes lc rgb"green" notitle,\
     "profit.dat" u ($1+d_width):3 w boxes lc rgb"red" notitle
This time we get a histogram with two group of data like this:

Plot histogram using boxes with more than one group of data

25 comments:

  1. Your gnuplot blogs are very useful.

    Could you explain what the parameters in these lines mean?

    plot "profit.dat" u 1:2 w boxes lc rgb"green" notitle,\
    "profit.dat" u ($1+d_width):3 w boxes lc rgb"red" notitle

    ReplyDelete
    Replies
    1. The full version of these two lines is:
      plot "profit.dat" using 1:2 with boxes linecolor rgb"green" notitle,\
      "profit.dat" using ($1+d_width):3 with boxes linecolor rgb"red" notitle
      where
      "using 1:2" means plot the graph using the first and second column,
      "with boxes" means plot with plotstyle---boxes,
      linecolor rgb"green" means plot with green color
      and at last "notitle" means the legend not plotted.

      Delete
    2. Gnuplot Surprising: Plot Histograms Using Boxes >>>>> Download Now

      >>>>> Download Full

      Gnuplot Surprising: Plot Histograms Using Boxes >>>>> Download LINK

      >>>>> Download Now

      Gnuplot Surprising: Plot Histograms Using Boxes >>>>> Download Full

      >>>>> Download LINK Gj

      Delete
  2. htmlResponseBytes 39842 66666
    cssResponseBytes 109265 191911
    imageResponseBytes 205179 222122
    javascriptResponseBytes 468573 476666
    otherResponseBytes 4326 4455

    I want to create graph for this data, how can i modify your above code.

    ReplyDelete
    Replies
    1. plot "file.dat" u 0:2 w boxes lc rgb"green" notitle
      set xtics ("html" 0,"css" 1, "image" 2, "javascript" 3,"other" 4)

      Delete
    2. Or simply
      plot "file.dat" u 0:2:xtic(1) w boxes lc rgb"green" notitle

      Delete
    3. Thank you for your post.

      But I don't understand how that is going to work. What is meant by plotting 0:2:xtic(1)? Is it possible to create a column diagramm with more than one group of data?

      Thank you in advance.

      Delete
    4. I think this means that the values on the x axis will be assigned dynamically, the values on the y axis will be taken from the 2nd column and the descriptions under the x axis will be taken from 1st column.

      It took me a while to figure out that in order to plot the second set of data it is necessary to refer to the x axis values using the $0, e.g.:

      plot datafile using 0:2:xtic(1) with boxes title "1. measurement" linecolor rgb "#7030a0",\
      datafile using ($0+offset):3 with boxes title "2. measurement" linecolor rgb "#00b050",\
      datafile using ($0+2*offset):4 with boxes title "3. measurement" linecolor rgb "#00b0f0"

      Delete
  3. In addition, it allows you to plot lines over histogram-like boxes. You cannot do it if you use real histograms (as far as I know).
    Thanks for the share !

    ReplyDelete
  4. Many thanks for this article! :)

    ReplyDelete
  5. very useful indeed

    ReplyDelete
  6. i want to plot the same histograms with fill pattern. please guide me.
    i have 3 columns with 11 values in each column. i want to give different patterns to columns so that it can be readable in gray scale printed form.

    ReplyDelete
    Replies
    1. "set style fill pattern
      plot sin(x) with boxes, cos(x) with boxes"
      will plot sin(x) and cos(x) with different fill patterns. And your problem can be solved similarly.

      Delete
  7. And if you want to make this graph from the command line, what should be the syntax?

    ReplyDelete
    Replies
    1. In Command Line : gnuplot file.p make sure u r in exact directory with dat file

      Delete
  8. I want to add my own data file but when i to it says no data to plot

    ReplyDelete
  9. What if we have more than 3 columns? Then how will we draw the graph

    ReplyDelete
  10. very useful blog post. Thank you.

    ReplyDelete
  11. Hi, I am new to gnuplot and would require your help. I want to plot a statistical histogram of 7 columns, having x at x-axis and frequency at Y axis. I have got the histogram using MATLAB, but I want to draw it using gnuplot. Example code of such histogram will be appriciated.
    Thanks

    ReplyDelete
  12. How can I overlay a gaussian curve fitted on hinstogram plot?

    ReplyDelete
  13. the post was really helpful, thank you

    ReplyDelete
  14. Is there any way I can plot without horizontal lines, something like this: https://www.oreilly.com/openbook/cgi/figs/cgi0606.gif

    ReplyDelete

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.