How do you plot bar charts in gnuplot with text labels?
To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.
To create a bar chart with pyplot, we use the plt. bar() function.
Running gnuplot is easy: from a command prompt on any system, type gnuplot. It is even possible to do this over a telnet or ssh connection, and preview the graphs in text mode!
What is gnuplot? gnuplot is a command-driven interactive function plotting program. It can be used to plot functions and data points in both two- and three- dimensional plots in many different formats. It is designed primarily for the visual display of scientific data.
Simple bar graph:
set boxwidth 0.5 set style fill solid plot "data.dat" using 1:3:xtic(2) with boxes
data.dat:
0 label 100 1 label2 450 2 "bar label" 75
If you want to style your bars differently, you can do something like:
set style line 1 lc rgb "red" set style line 2 lc rgb "blue" set style fill solid set boxwidth 0.5 plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \ "data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2
If you want to do multiple bars for each entry:
data.dat:
0 5 0.5 6 1.5 3 2 7 3 8 3.5 1
gnuplot:
set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,) set boxwidth 0.5 set style fill solid plot 'data.dat' every 2 using 1:2 with boxes ls 1,\ 'data.dat' every 2::1 using 1:2 with boxes ls 2
If you want to be tricky and use some neat gnuplot tricks:
Gnuplot has psuedo-columns that can be used as the index to color:
plot 'data.dat' using 1:2:0 with boxes lc variable
Further you can use a function to pick the colors you want:
mycolor(x) = ((x*11244898) + 2851770) plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable
Note: you will have to add a couple other basic commands to get the same effect as the sample images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With