1#!/usr/local/bin/python 2 3# Copyright (C) 2001-2017 Alan W. Irwin 4 5# Demo of multiple stream/window capability. 6# 7# This file is part of PLplot. 8# 9# PLplot is free software; you can redistribute it and/or modify 10# it under the terms of the GNU Library General Public License as published 11# by the Free Software Foundation; either version 2 of the License, or 12# (at your option) any later version. 13# 14# PLplot is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU Library General Public License for more details. 18# 19# You should have received a copy of the GNU Library General Public License 20# along with PLplot; if not, write to the Free Software 21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22# 23 24 25# Append to effective python path so that can find plplot modules. 26from plplot_python_start import * 27 28import sys 29import plplot as w 30 31# Parse and process command line arguments 32w.plparseopts(sys.argv, w.PL_PARSE_FULL) 33 34# Simple line plot and multiple windows demo. 35 36from numpy import * 37 38def main(w): 39 40 geometry_master = "500x410+100+200" 41 geometry_slave = "500x410+650+200" 42 driver = w.plgdev() 43 (fam, num, bmax) = w.plgfam() 44 45 print("Demo of multiple output streams via the %s driver." % driver) 46 print("Running with the second stream as slave to the first.") 47 print("") 48 49 # Set up the first stream. 50 w.plsetopt("geometry", geometry_master) 51 w.plsdev(driver) 52 w.plssub(2, 2) 53 w.plinit() 54 55 # Start next stream. 56 w.plsstrm(1) 57 58 # Turn off pause to make this a slave (must follow master) 59 60 w.plsetopt("geometry", geometry_slave) 61 w.plspause(0) 62 w.plsdev(driver) 63 w.plsfam(fam, num, bmax) 64 w.plsetopt("fflen","2") 65 w.plinit() 66 67 # Set up the data & plot 68 # Original case 69 70 w.plsstrm(0) 71 72 xscale = 6. 73 yscale = 1. 74 xoff = 0. 75 yoff = 0. 76 plot1(w, xscale, yscale, xoff, yoff) 77 78 # Set up the data & plot 79 80 xscale = 1. 81 yscale = 1.e+6 82 plot1(w, xscale, yscale, xoff, yoff) 83 84 # Set up the data & plot 85 86 xscale = 1. 87 yscale = 1.e-6 88 digmax = 2 89 w.plsyax(digmax, 0) 90 plot1(w, xscale, yscale, xoff, yoff) 91 92 # Set up the data & plot 93 94 xscale = 1. 95 yscale = 0.0014 96 yoff = 0.0185 97 digmax = 5 98 w.plsyax(digmax, 0) 99 plot1(w, xscale, yscale, xoff, yoff) 100 101 # To slave 102 # The w.pleop() ensures the eop indicator gets lit. 103 104 w.plsstrm(1) 105 plot4(w) 106 w.pleop() 107 108 # Back to master 109 110 w.plsstrm(0) 111 plot2(w) 112 plot3(w) 113 114 # To slave 115 116 w.plsstrm(1) 117 plot5(w) 118 w.pleop() 119 120 # Back to master to wait for user to advance 121 122 w.plsstrm(0) 123 w.pleop() 124 # Restore defaults 125 # Probably only need to do that for stream 0, but not quite sure about 126 # that and no need to do this currently in any case (since example 14 not used from 127 # pyqtdemo or python_demos.py) so don't restore defaults for now. 128# =============================================================== 129 130def plot1(w, xscale, yscale, xoff, yoff): 131 132 x = xoff + (xscale/60.)*(1+arange(60)) 133 y = yoff + yscale*pow(x,2.) 134 135 xmin = x[0] 136 xmax = x[59] 137 ymin = y[0] 138 ymax = y[59] 139 140 xs = x[3::10] 141 ys = y[3::10] 142 143 # Set up the viewport and window using w.plenv. The range in X 144 # is 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes 145 # are scaled separately (just = 0), and we just draw a 146 # labelled box (axis = 0). 147 148 w.plcol0(1) 149 w.plenv(xmin, xmax, ymin, ymax, 0, 0) 150 w.plcol0(6) 151 w.pllab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2") 152 153 # Plot the data points 154 155 w.plcol0(9) 156 w.plpoin(xs, ys, 9) 157 158 # Draw the line through the data 159 160 w.plcol0(4) 161 w.plline(x, y) 162 w.plflush() 163 164# =============================================================== 165 166def plot2(w): 167 168 # Set up the viewport and window using w.plenv. The range in X 169 # is -2.0 to 10.0, and the range in Y is -0.4 to 2.0. The axes 170 # are scaled separately (just = 0), and we draw a box with 171 # axes (axis = 1). 172 173 w.plcol0(1) 174 w.plenv(-2.0, 10.0, -0.4, 1.2, 0, 1) 175 w.plcol0(2) 176 w.pllab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function") 177 178 # Fill up the arrays 179 180 x = (arange(100)-19)/6.0 181 if 0.0 in x: 182 #replace 0.0 by small value that gives the same sinc(x) result. 183 x[list(x).index(0.0)] = 1.e-30 184 y = sin(x)/x 185 186 # Draw the line 187 188 w.plcol0(3) 189 w.plline(x, y) 190 w.plflush() 191 192# =============================================================== 193 194def plot3(w): 195 196 # For the final graph we wish to override the default tick 197 # intervals, so do not use w.plenv 198 199 w.pladv(0) 200 201 # Use standard viewport, and define X range from 0 to 360 202 # degrees, Y range from -1.2 to 1.2. 203 204 w.plvsta() 205 w.plwind(0.0, 360.0, -1.2, 1.2) 206 207 # Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y. 208 209 w.plcol0(1) 210 w.plbox("bcnst", 60.0, 2, "bcnstv", 0.2, 2) 211 212 # Superimpose a dashed line grid, with 1.5 mm marks and spaces. 213 214 w.plstyl([1500], [1500]) 215 w.plcol0(2) 216 w.plbox("g", 30.0, 0, "g", 0.2, 0) 217 w.plstyl([], []) 218 219 w.plcol0(3) 220 w.pllab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function") 221 222 x = 3.6*arange(101) 223 y = sin((pi/180.)*x) 224 225 w.plcol0(4) 226 w.plline(x, y) 227 w.plflush() 228 229# =============================================================== 230 231def plot4(w): 232 233 dtr = pi / 180.0 234 x0 = cos(dtr*arange(361)) 235 y0 = sin(dtr*arange(361)) 236 237 # Set up viewport and window, but do not draw box 238 239 w.plenv(-1.3, 1.3, -1.3, 1.3, 1, -2) 240 241 i = 0.1*arange(1,11) 242 #outerproduct(i,x0) and outerproduct(i,y0) is what we are 243 #mocking up here since old Numeric version does not have outerproduct. 244 i.shape = (-1,1) 245 x=i*x0 246 y=i*y0 247 248 # Draw circles for polar grid 249 for i in range(10): 250 w.plline(x[i], y[i]) 251 252 w.plcol0(2) 253 for i in range(12): 254 theta = 30.0 * i 255 dx = cos(dtr * theta) 256 dy = sin(dtr * theta) 257 258 # Draw radial spokes for polar grid 259 260 w.pljoin(0.0, 0.0, dx, dy) 261 262 # Write labels for angle 263 264 text = str(int(theta)) 265#Slightly off zero to avoid floating point logic flips at 90 and 270 deg. 266 if dx >= -0.00001: 267 w.plptex(dx, dy, dx, dy, -0.15, text) 268 else: 269 w.plptex(dx, dy, -dx, -dy, 1.15, text) 270 271 # Draw the graph 272 273 r = sin((dtr*5.)*arange(361)) 274 x = x0*r 275 y = y0*r 276 277 w.plcol0(3) 278 w.plline(x, y) 279 280 w.plcol0(4) 281 w.plmtex("t", 2.0, 0.5, 0.5, "#frPLplot Example 3 - r(#gh)=sin 5#gh") 282 w.plflush() 283 284# =============================================================== 285 286XPTS = 35 287YPTS = 46 288XSPA = 2./(XPTS-1) 289YSPA = 2./(YPTS-1) 290 291tr = array((XSPA, 0.0, -1.0, 0.0, YSPA, -1.0)) 292 293def mypltr(x, y, data): 294 result0 = data[0] * x + data[1] * y + data[2] 295 result1 = data[3] * x + data[4] * y + data[5] 296 return array((result0, result1)) 297 298def plot5(w): 299 300 mark = 1500 301 space = 1500 302 303 clevel = -1. + 0.2*arange(11) 304 305 xx = (arange(XPTS) - XPTS//2) / float((XPTS//2)) 306 yy = (arange(YPTS) - YPTS//2) / float((YPTS//2)) - 1. 307 xx.shape = (-1,1) 308 z = (xx*xx)-(yy*yy) 309 # 2.*outerproduct(xx,yy) for new versions of Numeric which have outerproduct. 310 w_array = 2.*xx*yy 311 312 w.plenv(-1.0, 1.0, -1.0, 1.0, 0, 0) 313 w.plcol0(2) 314 w.plcont(z, clevel, mypltr, tr) 315 w.plstyl([mark], [space]) 316 w.plcol0(3) 317 w.plcont(w_array, clevel, mypltr, tr) 318 w.plstyl([], []) 319 w.plcol0(1) 320 w.pllab("X Coordinate", "Y Coordinate", "Streamlines of flow") 321 w.plflush() 322 323# =============================================================== 324 325main(w) 326w.plend() 327