Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Last active September 22, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/7d0281adf05d1f35dce5721263a372f0 to your computer and use it in GitHub Desktop.
Save smailliwcs/7d0281adf05d1f35dce5721263a372f0 to your computer and use it in GitHub Desktop.
Gradients in gnuplot
set terminal png size 640,480
set output 'gradient.png'
set xrange [-5:5]
set yrange [-5:5]
set cbrange [0:1]
unset colorbox
unset key
set palette gray negative
set object 1 rectangle from 1,-10 to 10,10 behind fillcolor rgb 'black' fillstyle noborder
plot '< python gradient.py -x -1 1 -10 10 64' with image
import argparse
import sys
def parseArgs():
parser = argparse.ArgumentParser(description = "Generate gradient data suitable for use in gnuplot.")
parser.add_argument("xMin", metavar = "X_MIN", type = float, help = "minimum x-coordinate")
parser.add_argument("xMax", metavar = "X_MAX", type = float, help = "maximum x-coordinate")
parser.add_argument("yMin", metavar = "Y_MIN", type = float, help = "minimum y-coordinate")
parser.add_argument("yMax", metavar = "Y_MAX", type = float, help = "maximum y-coordinate")
parser.add_argument("stepCount", metavar = "STEPS", type = int, help = "number of steps")
axis = parser.add_mutually_exclusive_group(required = True)
axis.add_argument("-x", action = "store_true", default = False, help = "grade along x-axis")
axis.add_argument("-y", action = "store_true", default = False, help = "grade along y-axis")
return parser.parse_args()
def grade(min, max, step, stepCount):
return min + step * (max - min) / stepCount
def write(x, y, z):
sys.stdout.write("{0} {1} {2}\n".format(x, y, z))
args = parseArgs()
for step in range(args.stepCount + 1):
if args.x:
x1 = x2 = grade(args.xMin, args.xMax, step, args.stepCount)
y1 = args.yMin
y2 = args.yMax
elif args.y:
x1 = args.xMin
x2 = args.xMax
y1 = y2 = grade(args.yMin, args.yMax, step, args.stepCount)
z = step / args.stepCount
write(x1, y1, z)
write(x2, y2, z)
sys.stdout.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment