3

I have to plot an histogram in logarithmic scale on both axis using gnuplot. I need bins to be equally spaced in log10. Using a logarithmic scale on the y axis isn't a problem. The main problem is creating the bin on the x axis. For example, using 10 bins in log10, first bins will be [1],[2],[3]....[10 - 19][20 - 29].....[100 190] and so on. I've searched on the net but I couldn't find any practical solution. If realizing it in gnuplot is too much complicated could you suggest some other software/language to do it?

As someone asked I will explain more specifically what I need to do. I have a (huge) list like this:

1   14000000
2   7000000
3   6500000
.
.
.
.
6600    1
8900    1
15000   1
19000   1

It shows, for example, that 14 milions of ip addresses have sent 1 packet, 7 milions 2 packets.... 1 ip address have sent 6600 packets, ... , 1 ip address have sent 19000 packets. As you can see the values on both axes are pretty high so I cannot plot it without a logarithmic scale.

The first things I tried because I needed to do it fast was plotting this list as it is with gnuplot setting logscale on both axes using boxes. The result is understandable but not too appropriate. In fact, the boxes became more and more thin going right on the x axis because, obviously, there are more points in 10-100 than in 1-10! So it became a real mess after the second decade.

papafe
  • 2,959
  • 4
  • 41
  • 72
  • 1
    Matplotlib with Python. It's an amazing tool. – Blender Oct 27 '11 at 18:55
  • What kind of data do you have that you want to plot a histogram with a logarithmic x-axis? Maybe an other approach makes more sense to plot your data with!? – Woltan Oct 28 '11 at 07:57
  • I have edited the question for better understanding. @Blender are you sure that it is possible to do it with that library? – papafe Oct 28 '11 at 15:27
  • I have just used the maplotlib @Blender was talking about and it is exactly what I needed! – papafe Oct 29 '11 at 22:54

3 Answers3

5

I tried plotting a histogram with both axis being logarithmically scaled and gnuplot through the error

Log scale on X is incompatible with histogram plots.

So it appears that gnuplot does not support a log scale on the x axis with histograms.

Woltan
  • 13,723
  • 15
  • 78
  • 104
  • Ok, that's an important thing to know! – papafe Oct 28 '11 at 15:28
  • This is certainly not true. GnuPlot, does support log-log plots. You can set the log-log scaling through `set logscale` and then you can plot your file with the data with the command `plot "file.txt" w p`. The assumption is that "file.txt" has two values per line (x and y), and there is no line in that file that has x = 0 or y = 0, since the logarithm is undefined there. Moreover, plotting with boxes is even trickier. It can work _only if_ the xrange is of the form [1:m], and all values for x between 1 and m are given and are non-zero. Otherwise, boxes of size 0 have to be generated => Error. – MightyMouse Dec 27 '12 at 23:14
  • See my example solution below. – MightyMouse Dec 27 '12 at 23:33
4

Plotting in log-log scale in GnuPlot is perfectly doable contrary to the other post in this thread.

One can set the log-log scale in GnuPlot with the command set logscale. Then, the assumption is that we have a file with positive (strictly non-zero) values both in the x-axis, as well as the y-axis. For example, the following file is a valid file:

1 0.5
2 0.2
3 0.15
4 0.05

After setting the log-log scale one can plot the file with the command: plot "file.txt" w p where of course file.txt is the name of the file. This command will generate the output with points.

Note also that plotting boxes is tricky and is probably not recommended. One first has to restrict the x-range with a command of the form set xrange [1:4] and only then plot with boxes. Otherwise, when the x-range is undefined an error is returned. I am assuming that in this case plot requires (for appropriate x-values) some boxes to have size log(0), which of course is undefined and hence the error is returned.

Hope it is clear and it will also help others.

MightyMouse
  • 13,208
  • 8
  • 33
  • 43
-1

Have you tried Matplotlib with Python? Matplotlib is a really nice plotting library and when used with Python's simple syntax, you can plot things quite easily:

import matplotlib.pyplot as plot

figure = plot.figure()
axis = figure.add_subplot(1 ,1, 1)
axis.set_yscale('log')

# Rest of plotting code
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thanks to a comment I started using matplotlib and it does exactly what I needed. I also used logspace to have logarithmic bins. Now what I'm trying to understand why normed=True of the hist function doesn't seem to work. – papafe Oct 31 '11 at 16:44
  • 1
    It's a confusing keyword and will be removed in the next major release of numpy (matplotlib's numerical backend). The documentation suggests to use something else: http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html#numpy.histogram – Blender Oct 31 '11 at 16:46