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

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

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

Plot histogram using boxes with more than one group of data |

Your gnuplot blogs are very useful.

ReplyDeleteCould 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

The full version of these two lines is:

Deleteplot "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.

what is $1

DeletehtmlResponseBytes 39842 66666

ReplyDeletecssResponseBytes 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.

plot "file.dat" u 0:2 w boxes lc rgb"green" notitle

Deleteset xtics ("html" 0,"css" 1, "image" 2, "javascript" 3,"other" 4)

Or simply

Deleteplot "file.dat" u 0:2:xtic(1) w boxes lc rgb"green" notitle

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).

ReplyDeleteThanks for the share !

Many thanks for this article! :)

ReplyDeletevery useful indeed

ReplyDeletei want to plot the same histograms with fill pattern. please guide me.

ReplyDeletei 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.

"set style fill pattern

Deleteplot 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.

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

ReplyDeleteI am sorry! But I just did not get your meaning.

DeleteIn Command Line : gnuplot file.p make sure u r in exact directory with dat file

DeleteI want to add my own data file but when i to it says no data to plot

ReplyDeleteWhat if we have more than 3 columns? Then how will we draw the graph

ReplyDeletevery useful blog post. Thank you.

ReplyDelete