Dynamically colored bar charts in Gnuplot? - gnuplot

Without choosing the color explicitly like Different coloured bars in gnuplot bar chart? is there a way for GNU plot to choose some distinguished colors based on the key (like a hash?)?
# git rev-list --count master
$commits << EOD
gecko 716280
webkit 226748
blink 906439
EOD
set terminal png
set yrange [0:*] # start at zero, find max from the data
set boxwidth 0.5 # use a fixed width for boxes
unset key # turn off all titles
set style fill solid # solid color boxes
set title 'commits'
plot '$commits' using 2:xtic(1) with boxes
Bonus: Instead of 1x10^6 (which I find odd), could it simple say 716k, 227k, 906k. I.e. the scale is in the 1000s for the Y axis.

The solution provided in Different coloured bars in gnuplot bar chart? works also without defining the linetypes. Gnuplot will use the default ones.
set yrange [0:*] # start at zero, find max from the data
set boxwidth 0.5 # use a fixed width for boxes
unset key # turn off all titles
set style fill solid # solid color boxes
set title 'commits'
plot '$commits' using 0:2:($0+1):xtic(1) with boxes lc variable
You can also use one of the other predefined sequences of colors adding the following line:
set colors {default|classic|podo}

Related

Manual scaling of axis (poltting with boxes) in gnuplot

I'm trying to plot a really long list, which represents frequency of some data. The x axis goes from 1 to 1881, and the y goes from 1 to 1978. I tried plotting with the following configurations:
set log y 5
set log x 32
set xtics 2
set ytics 5
plot "freq.dat" w boxes
But i get the following result:
Which is clearly not good because there are these intersections of the boxes. I want to have some kind of scale that have a lot more space between 10 and 150 than the outside of this area. How can i do that? I've tried every combination of logs and xtics and couldn't make it look good. The y axis seems good to me, the only problem is the spacing of x-axis.
Also, i want to know how to do this in gnuplot specifically (if possible).
Thanks.
It seems to me that your script overrides somewhere the default boxwidth. The default behavior is:
By default, adjacent boxes are extended in width until they touch
each other. A different default width may be specified using the set boxwidth command. Relative widths are interpreted as being a
fraction of this default width.
So for example this:
set terminal pngcairo enhanced
set output 'fig.png'
set log x 2
set log y 5
set yr [0.2:3125]
set xr [1:512]
set boxwidth 1 relative
set xtics 1,2,512 out nomirror
set ytics 1,5,3125 out nomirror
unset key
#set style fill pattern 6
plot 'freq.dat' w boxes lc rgb 'royalblue' lw 1.1
then yields:

Gnuplot bar diagram different color with value on top of bar

My data set is simple:
CPU 5.7
Memory 3.7
I want to plot a simple bar diagram with different colors for each value and the corresponding values should be shown on top of each bar. I also want to plot the ylabel and the legend. It should almost look like the the following diagram:
Is this possible in gnuplot? There seems to be hardly any document for doing this in gnuplot. Plotting bars with historgram seems easy but styling with different colors, the value on top and the legends part is turning out to be a bit tricky for me. Can someone please help me?
Thanks in advance.
Maybe the following comes quite close:
This is the gnuplot script:
set terminal pngcairo
set output "data.png"
set title "CPU and Memory"
set nokey
set boxwidth 0.8
set style fill solid
set xrange [-1:2]
set xtics nomirror
set yrange [0:7]
set grid y
set ylabel "Time"
plot "data.dat" using 0:2:3:xtic(1) with boxes lc rgb var ,\
"data.dat" using 0:($2+0.5):2 with labels
The pseudo column 0, which is the current line number, is used as x value.
I have added a third column to your data which contains the color as rgb value.
The value on top of the bars is printed by the with labels command. It requires a using with three values: x, y, string. The part ($2+0.5) takes the y-value from the second column and adds 0.5.
The identifiers "CPU" and "Memory" are printed below the corresponding bar instead of using a separate key.
And this is the modified datafile:
CPU 5.7 0x4472c4
Memory 3.7 0xed7d31

How to label the x axis with exact values

I have drawn the bar chart above using the following code:
set terminal jpeg medium
set output "bar.jpeg" # shall be the name of the chart
set xlabel "full configuration time in msec"
set ylabel "Task rejection rate (%)"
set boxwidth 0.5
set style fill solid
#set autoscale
plot "data.data" using 1:2 with boxes
My file data.data looks like this:
2 9
5 24
7 46
10 66
15 100
I want the exact values 2,5,7,10,15 to appear along the x axis with the corresponding value along the y axis and to use a color other than red. What changes do I have to make to my code? I also need to remove that "data.data" using 1:2 from the top right corner...
Any suggestions?
To remove the key, use unset key.
The markers at the bottom of the graph are called xtics. If you want them to appear every 1, rather than every 2, then you could use set xtics 1. Depending on exactly what you wanted to do, you can customise the xtics even more. In gnuplot if you do help xtics there's plenty of information on that.
To change the colour of your boxes, you can use the lc (line colour) property. I have used a hexadecimal #RRGGBB format but you can also use names of colours like green, blue, etc. Look at help linecolor for more info on that.
Incorporating all of those changes into your script:
set xlabel "full configuration time in msec"
set ylabel "Task rejection rate (%)"
set boxwidth 0.5
set style fill solid
unset key
set xtics 1
plot "data.data" using 1:2 with boxes lc rgb '#52bb23'
By the way, I used the pngcairo terminal rather than the jpeg one as I think it looks better. Try set term to see what terminals are available to you.
In addition to #TomFenech's answer: If you want to show only the x-values specified in the data file, you can use the xtic function. Note, that in this case the defined x-format has no effect and the value is taken 'as-is':
set xlabel "Full configuration time in msec"
set ylabel "Task rejection rate (%)"
set boxwidth 0.5
set style fill solid
set xtics out nomirror
plot "data.data" using 1:2:xtic(1) with boxes notitle

Creating a microphone polar pattern plot in gnuplot

I would like to create a microphone polar pattern plot that has a scale of -20 (in the center) out to +5 in steps of 5. I have found similar code but nothing that allows for the scales to be negative.
Multiple patterns will then need to added to the plot covering a few different frequencies, I have degree values (0-360) and corresponding dB values (-25 - +5).
This is what the plot should look like (though with slightly different scales):
The closest gnuplot I have found to this is here: How to get a radial(polar) plot using gnu plot?
Perhaps this could be modified to suit my needs?
I would also like 0 degrees to be found at the top of the plot rather than on the right.
I am new to using gnuplot so I am not particularly familiar with its code, therefore it has been difficult for me to modify the code with any great success (so far anyway).
So you want to plot a polar function, e.g. r(theta) = 1 + sin(theta).
Plotting the function is quite easy, just do
set polar
plot 1+sin(t)
A simple polar grid can be plotted with
set grid polar
but that has the raxis and the rtics on a different position than where you wanted. It is not problem to specify custom labels. But angular labels aren't supported, so you need to set them manually. And the border and the other axes and tics must be unset.
To get the very same image as you showed, use the following script:
set terminal pngcairo size 700,600 font ',10'
set output 'cardioid.png'
set angle degree
set polar
set size ratio 1
set tmargin 3
set bmargin 3
set style line 11 lc rgb 'gray80' lt -1
set grid polar ls 11
unset border
unset xtics
unset ytics
r=1
set rrange [0:r]
set rtics 0.166 format '' scale 0
set label '0°' center at first 0, first r*1.05
set label '180°' center at first 0, first -r*1.05
set label '90°' right at first -r*1.05, 0
set label '270°' left at first r*1.05, 0
set for [i=1:5] label at first r*0.02, first r*((i/6.0) + 0.03) sprintf("%d dB", -30+(i*5))
unset raxis
plot 0.5*(1+sin(t)) linewidth 2 t ''
With the result:
That includes some offsets for the labels, which depend on the terminal, the canvas size and the font size. So you may need to adapt them.
I had to increase the top and bottom margins a bit (here by 3 character heights) in order to have enough space for the angular labels. They aren't included in the automatic margin calculations, because the don't belong to an axis.
Unfortunately Christoph's answer is wrong.
You can see that if you check where the plot curve crosses the 5db circle.
What should be plotted is
20*log10(A+B*cos(t))
where A+B = 1 and A - B determines the (nominal) directivity pattern.
The first diagram seems to be for A=B=0.5 which makes for a cardioid pattern.

Hide the boxes in a histogram with errorbars in gnuplot?

I have a gnuplot histogram with errorbars. How can I hide the boxes so just the errorbars remain?
I can achieve this effect for a single data set using the plain errorbars style and hiding the marker at the center, but I would like to use the histogram style to show multiple series on the same plot.
My code currently looks like this:
set style data histograms
set style histogram errorbars lw 2
plot "mydata.dat" using $2:$3:xtic(1) title "Series1", \
"" using $4:$5 title "Series2"
The following lines make the boxes too small to appear at typical zoom levels, but I'd prefer to explicitly disable them.
set boxwidth 0.00001
set style fill solid noborder
Don't do:
set style data histograms
Instead just do:
plot "mydata.dat" with errorbars
If this does not solve your question, can you point me to an image of a plot similar to what you are trying to achieve?

Resources