-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
executable file
·111 lines (93 loc) · 4.23 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! /usr/bin/env python
# Copyright (C) 2021 Joaquin Abian <gatoygata2@gmail.com>
# Copyright (C) 1999-2003 Michael Haggerty <mhagger@alum.mit.edu>
#
# This file is licensed under the GNU Lesser General Public License
# (LGPL). See LICENSE.txt for details.
"""demo.py -- Demonstrate the Gnuplot python module.
Run this demo by typing 'python demo.py'. For a more complete test of
the Gnuplot package, see test.py.
"""
import numpy as np
# If the package has been installed correctly, this should work:
import gnuplot
import gnuplot.funcutils
def demo():
"""Demonstrate the Gnuplot package."""
# A straightforward use of gnuplot. The `debug=1' switch is used
# in these examples so that the commands that are sent to gnuplot
# are also output on stderr.
g = gnuplot.Gnuplot(debug=1)
g.title('A simple example') # (optional)
g('set style data linespoints') # give gnuplot an arbitrary command
# Plot a list of (x, y) pairs (tuples or a numpy array would
# also be OK):
g.plot([[0, 1.1], [1, 5.8], [2, 3.3], [3, 4.2]])
input('Please press return to continue...\n')
g.reset()
# Plot one dataset from an array and one via a gnuplot function;
# also demonstrate the use of item-specific options:
x = np.arange(10, dtype='float_')
y1 = x**2
# Notice how this plotitem is created here but used later? This
# is convenient if the same dataset has to be plotted multiple
# times. It is also more efficient because the data need only be
# written to a temporary file once.
d = gnuplot.Data(x, y1,
title='calculated by python',
with_='points lt 3 pt 3')
g.title('Data can be computed by python or gnuplot')
g.xlabel('x')
g.ylabel('x squared')
# Plot a function alongside the Data PlotItem defined above:
g.plot(gnuplot.Func('x**2', title='calculated by gnuplot'), d)
input('Please press return to continue...\n')
# Save what we just plotted as a color postscript file.
# With the enhanced postscript option, it is possible to show `x
# squared' with a superscript (plus much, much more; see `help set
# term postscript' in the gnuplot docs). If your gnuplot doesn't
# support enhanced mode, set `enhanced=0' below.
g.ylabel('x^2') # take advantage of enhanced postscript mode
g.hardcopy('gp_test.ps', enhanced=1, color=1)
print('\n******** Saved plot to postscript file "gp_test.ps" ********\n')
input('Please press return to continue...\n')
g.reset()
# Demonstrate a 3-d plot:
# set up x and y values at which the function will be tabulated:
x = np.arange(35)/2.0
y = np.arange(30)/10.0 - 1.5
# Make a 2-d array containing a function of x and y. First create
# xm and ym which contain the x and y values in a matrix form that
# can be `broadcast' into a matrix of the appropriate shape:
xm = x[:, np.newaxis]
ym = y[np.newaxis, :]
m = (np.sin(xm) + 0.1*xm) - ym**2
g('set parametric')
g('set style data lines')
g('set hidden')
g('set contour base')
g.title('An example of a surface plot')
g.xlabel('x')
g.ylabel('y')
# The `binary=1' option would cause communication with gnuplot to
# be in binary format, which is considerably faster and uses less
# disk space. (This only works with the splot command due to
# limitations of gnuplot.) `binary=1' is the default, but here we
# disable binary because older versions of gnuplot don't allow
# binary data. Change this to `binary=1' (or omit the binary
# option) to get the advantage of binary format.
g.splot(gnuplot.GridData(m, x, y, binary=0))
input('Please press return to continue...\n')
# plot another function, but letting GridFunc tabulate its values
# automatically. f could also be a lambda or a global function:
def f(x, y):
return 1 / (1 + 0.01 * x**2 + 0.5 * y**2)
g.splot(gnuplot.funcutils.compute_GridData(x, y, f, binary=0))
input('Please press return to continue...\n')
# Explicit delete shouldn't be necessary, but if you are having
# trouble with temporary files being left behind, try uncommenting
# the following:
# del g, d
# when executed, just run demo():
if __name__ == '__main__':
demo()