#!/usr/bin/env python import click import subprocess @click.command() @click.option("-t", "--title", default="", help="Title for the diagram.") @click.option("-o", "--output", help="Output PNG file path") @click.option("-s", "--samples", default=1000, help="Number of samples to take.") @click.option("-x", "--xlabel", default="X", help="Label for the X axis.") @click.option("-y", "--ylabel", default="Y", help="Label for the Y axis.") @click.option("--y2label", default="", help="Label for the Y2 axis.") @click.option("--logy", default=False, help="Use logarithmic scale for Y.") @click.option("--key", default="above right width 3", help="Key command.") @click.argument("expression", default="sin(x)") def iplot(output, samples, expression, xlabel, ylabel, y2label, logy, title, key): """Draws PNG diagrams using Kitty protocol and gnuplot.""" fg = 'white' if output: fg = 'black' command = ( f""" set terminal pngcairo transparent truecolor enhanced font 'Fira Sans,20' size 1900,900 set autoscale set samples {samples} set output '{'|tpix -f' if not output else output}' set datafile separator "," set key {key} textcolor rgb "{fg}" set border lw 3 lc rgb "#333333" set xtics textcolor rgb "{fg}" set ytics textcolor rgb "{fg}" {f'set y2tics textcolor rgb "{fg}"' if y2label else ''} set xlabel "{xlabel}" textcolor rgb "{fg}" set ylabel "{ylabel}" textcolor rgb "{fg}" {f'set y2label "{y2label}" textcolor rgb "{fg}"' if y2label else ''} {f'set logscale y 10' if logy else ''} {f"set title '{title}' font 'Sans Serif,24' textcolor '{fg}'" if title else ''} set linetype 1 lc rgb "#55ee55" lw 3 set linetype 2 lc rgb "#ee5555" lw 3 set linetype 3 lc rgb "#7777ee" lw 3 set linetype 4 lc rgb "#ee55ee" lw 3 set linetype 5 lc rgb "#eeee33" lw 3 set style fill solid 1 border lt -1 sinc(x) = sin(x) / x square(x) = sgn(sin(x)) pulse(x) = (square(x) + 1) / 2 sinhz(x) = sin(2 * pi * x) coshz(x) = cos(2 * pi * x) squarehz(x) = square(2 * pi * x) pulsehz(x) = pulse(2 * pi * x) plot {expression} set output '/dev/null' """.replace( "\n ", "\n" ).strip() + "\n" ) subprocess.run(["gnuplot"], shell=True, input=command.encode("utf8")) if __name__ == "__main__": iplot()