2
votes

I'm plotting some histograms with GnuplotPy, and I'd like to change the color of the histogram bars. (By default, the bars are red.) The answers in this StackOverflow post gave several options for how to change histogram bar colors in plain Gnuplot, but I haven't been able to get these solutions to work in GnuplotPy.

Here's the basic setup that I'm using:

import Gnuplot

gp = Gnuplot.Gnuplot(persist = 1)
gp('set ylabel "Accuracy"')
gp('set format x " "') #remove the auto-generated xtics labels 
gp('set xtics scale 0') #remove xtic marks. 
gp(r'set xtics add ("Method 1" 0.15) ("Method 2" 1.15)')
dataToPlot = [6.9, 47.6]

gp('set style data histograms')
gp('set style fill solid 1.0 border -1')
plot1 = Gnuplot.PlotItems.Data(dataToPlot)

gp.plot(plot1)
gp.hardcopy('hist_example.eps',terminal = 'postscript', enhanced=1, color=1) #must come after plot() function
gp.reset() #must be placed after hardcopy()

The above code doesn't make any attempt to set the histogram bar color, and it produces a histogram with red bars:

enter image description here


I tried couple of things to change the histogram bar colors to blue, but none of them have been successful.

First, following mgilson's advice in this thread, I tried adding the following line, but it throws an error and the bars still come out red:

gp('set style lc rgb "blue"') #throws this error: line 0: expecting 'data', 'function', 'line', 'fill' or 'arrow'

I also tried the following modification to the code, and it threw an error without producing a plot:

plot1 = Gnuplot.PlotItems.Data(dataToPlot, with_="linecolor rgb 'blue'")  #throws this error: plot "/var/folders/98/st7ct15n227g1p92mljkx93m0000gq/T/tmp875mRk.gnuplot/fifo" notitle with linecolor rgb 'blue' ^ line 0: expecting 'lines', 'points', 'linespoints', 'dots', 'impulses',    'yerrorbars', 'xerrorbars', 'xyerrorbars', 'steps', 'fsteps',   'histeps', 'filledcurves', 'boxes', 'boxerrorbars', 'boxxyerrorbars',   'vectors', 'financebars', 'candlesticks', 'errorlines', 'xerrorlines',  'yerrorlines', 'xyerrorlines', 'pm3d', 'labels', 'histograms',   'image', 'rgbimage'

So, how can I change the histogram bar colors in GnuplotPy?

1
What if you try gp('set style line 1 lc rgb "blue"')?mgilson
An alternative hack that might work would be: plot1 = Gnuplot.PlotItems.Data(dataToPlot, with_="histograms linecolor rgb 'blue'"), Note that lc and linecolor mean the same thing to gnuplot.mgilson
plot1 = Gnuplot.PlotItems.Data(dataToPlot, with_="histograms linecolor rgb 'blue'") works! Feel free to write it as an answer, and I'll accept it.solvingPuzzles

1 Answers

1
votes

An alternative hack that will probably work would be:

plot1 = Gnuplot.PlotItems.Data(dataToPlot, with_="histograms linecolor rgb 'blue'")

It seems to me that gnuplot-py should allow some syntax like:

plot1 = Gnuplot.PlotItems.Data(dataToPlot, with_="histograms",linecolor="rgb 'blue'")

or something like that. I don't have a good test case to experiment with right now ...