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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
|
# -*- coding: utf-8 -*-
# pylint: disable=E1101, C0330, C0103
# E1101: Module X has no Y member
# C0330: Wrong continued indentation
# C0103: Invalid attribute/variable/method name
"""
polyobjects.py
==============
This contains all of the PolyXXX objects used by :mod:`wx.lib.plot`.
"""
__docformat__ = "restructuredtext en"
# Standard Library
import time as _time
import wx
import warnings
from collections import namedtuple
# Third-Party
try:
import numpy as np
except:
msg = """
This module requires the NumPy module, which could not be
imported. It probably is not installed (it's not part of the
standard Python distribution). See the Numeric Python site
(http://numpy.scipy.org) for information on downloading source or
binaries."""
raise ImportError("NumPy not found.\n" + msg)
# Package
from .utils import pendingDeprecation
from .utils import TempStyle
from .utils import pairwise
class PolyPoints(object):
"""
Base Class for lines and markers.
:param points: The points to plot
:type points: list of ``(x, y)`` pairs
:param attr: Additional attributes
:type attr: dict
.. warning::
All methods are private.
"""
def __init__(self, points, attr):
self._points = np.array(points).astype(np.float64)
self._logscale = (False, False)
self._absScale = (False, False)
self._symlogscale = (False, False)
self._pointSize = (1.0, 1.0)
self.currentScale = (1, 1)
self.currentShift = (0, 0)
self.scaled = self.points
self.attributes = {}
self.attributes.update(self._attributes)
for name, value in attr.items():
if name not in self._attributes.keys():
err_txt = "Style attribute incorrect. Should be one of {}"
raise KeyError(err_txt.format(self._attributes.keys()))
self.attributes[name] = value
@property
def logScale(self):
"""
A tuple of ``(x_axis_is_log10, y_axis_is_log10)`` booleans. If a value
is ``True``, then that axis is plotted on a logarithmic base 10 scale.
:getter: Returns the current value of logScale
:setter: Sets the value of logScale
:type: tuple of bool, length 2
:raises ValueError: when setting an invalid value
"""
return self._logscale
@logScale.setter
def logScale(self, logscale):
if not isinstance(logscale, tuple) or len(logscale) != 2:
raise ValueError("`logscale` must be a 2-tuple of bools")
self._logscale = logscale
def setLogScale(self, logscale):
"""
Set to change the axes to plot Log10(values)
Value must be a tuple of booleans (x_axis_bool, y_axis_bool)
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PolyPoints.logScale`
property instead.
"""
pendingDeprecation("self.logScale property")
self._logscale = logscale
@property
def symLogScale(self):
"""
.. warning::
Not yet implemented.
A tuple of ``(x_axis_is_SymLog10, y_axis_is_SymLog10)`` booleans.
If a value is ``True``, then that axis is plotted on a symmetric
logarithmic base 10 scale.
A Symmetric Log10 scale means that values can be positive and
negative. Any values less than
:attr:`~wx.lig.plot.PolyPoints.symLogThresh` will be plotted on
a linear scale to avoid the plot going to infinity near 0.
:getter: Returns the current value of symLogScale
:setter: Sets the value of symLogScale
:type: tuple of bool, length 2
:raises ValueError: when setting an invalid value
.. notes::
This is a simplified example of how SymLog works::
if x >= thresh:
x = Log10(x)
elif x =< thresh:
x = -Log10(Abs(x))
else:
x = x
.. seealso::
+ :attr:`~wx.lib.plot.PolyPoints.symLogThresh`
+ See http://matplotlib.org/examples/pylab_examples/symlog_demo.html
for an example.
"""
return self._symlogscale
# TODO: Implement symmetric log scale
@symLogScale.setter
def symLogScale(self, symlogscale, thresh):
raise NotImplementedError("Symmetric Log Scale not yet implemented")
if not isinstance(symlogscale, tuple) or len(symlogscale) != 2:
raise ValueError("`symlogscale` must be a 2-tuple of bools")
self._symlogscale = symlogscale
@property
def symLogThresh(self):
"""
.. warning::
Not yet implemented.
A tuple of ``(x_thresh, y_thresh)`` floats that define where the plot
changes to linear scale when using a symmetric log scale.
:getter: Returns the current value of symLogThresh
:setter: Sets the value of symLogThresh
:type: tuple of float, length 2
:raises ValueError: when setting an invalid value
.. notes::
This is a simplified example of how SymLog works::
if x >= thresh:
x = Log10(x)
elif x =< thresh:
x = -Log10(Abs(x))
else:
x = x
.. seealso::
+ :attr:`~wx.lib.plot.PolyPoints.symLogScale`
+ See http://matplotlib.org/examples/pylab_examples/symlog_demo.html
for an example.
"""
return self._symlogscale
# TODO: Implement symmetric log scale threshold
@symLogThresh.setter
def symLogThresh(self, symlogscale, thresh):
raise NotImplementedError("Symmetric Log Scale not yet implemented")
if not isinstance(symlogscale, tuple) or len(symlogscale) != 2:
raise ValueError("`symlogscale` must be a 2-tuple of bools")
self._symlogscale = symlogscale
@property
def absScale(self):
"""
A tuple of ``(x_axis_is_abs, y_axis_is_abs)`` booleans. If a value
is ``True``, then that axis is plotted on an absolute value scale.
:getter: Returns the current value of absScale
:setter: Sets the value of absScale
:type: tuple of bool, length 2
:raises ValueError: when setting an invalid value
"""
return self._absScale
@absScale.setter
def absScale(self, absscale):
if not isinstance(absscale, tuple) and len(absscale) == 2:
raise ValueError("`absscale` must be a 2-tuple of bools")
self._absScale = absscale
@property
def points(self):
"""
Get or set the plotted points.
:getter: Returns the current value of points, adjusting for the
various scale options such as Log, Abs, or SymLog.
:setter: Sets the value of points.
:type: list of `(x, y)` pairs
.. Note::
Only set unscaled points - do not perform the log, abs, or symlog
adjustments yourself.
"""
data = np.array(self._points, copy=True) # need the copy
# TODO: get rid of the
# need for copy
# work on X:
if self.absScale[0]:
data = self._abs(data, 0)
if self.logScale[0]:
data = self._log10(data, 0)
if self.symLogScale[0]:
# TODO: implement symLogScale
# Should symLogScale override absScale? My vote is no.
# Should symLogScale override logScale? My vote is yes.
# - symLogScale could be a parameter passed to logScale...
pass
# work on Y:
if self.absScale[1]:
data = self._abs(data, 1)
if self.logScale[1]:
data = self._log10(data, 1)
if self.symLogScale[1]:
# TODO: implement symLogScale
pass
return data
@points.setter
def points(self, points):
self._points = points
def _log10(self, data, index):
""" Take the Log10 of the data, dropping any negative values """
data = np.compress(data[:, index] > 0, data, 0)
data[:, index] = np.log10(data[:, index])
return data
def _abs(self, data, index):
""" Take the Abs of the data """
data[:, index] = np.abs(data[:, index])
return data
def boundingBox(self):
"""
Returns the bouding box for the entire dataset as a tuple with this
format::
((minX, minY), (maxX, maxY))
:returns: boundingbox
:rtype: numpy array of ``[[minX, minY], [maxX, maxY]]``
"""
if len(self.points) == 0:
# no curves to draw
# defaults to (-1,-1) and (1,1) but axis can be set in Draw
minXY = np.array([-1.0, -1.0])
maxXY = np.array([1.0, 1.0])
else:
minXY = np.minimum.reduce(self.points)
maxXY = np.maximum.reduce(self.points)
return minXY, maxXY
def scaleAndShift(self, scale=(1, 1), shift=(0, 0)):
"""
Scales and shifts the data for plotting.
:param scale: The values to scale the data by.
:type scale: list of floats: ``[x_scale, y_scale]``
:param shift: The value to shift the data by. This should be in scaled
units
:type shift: list of floats: ``[x_shift, y_shift]``
:returns: None
"""
if len(self.points) == 0:
# no curves to draw
return
# TODO: Can we remove the if statement alltogether? Does
# scaleAndShift ever get called when the current value equals
# the new value?
# cast everything to list: some might be np.ndarray objects
if (list(scale) != list(self.currentScale)
or list(shift) != list(self.currentShift)):
# update point scaling
self.scaled = scale * self.points + shift
self.currentScale = scale
self.currentShift = shift
# else unchanged use the current scaling
def getLegend(self):
return self.attributes['legend']
def getClosestPoint(self, pntXY, pointScaled=True):
"""
Returns the index of closest point on the curve, pointXY,
scaledXY, distance x, y in user coords.
if pointScaled == True, then based on screen coords
if pointScaled == False, then based on user coords
"""
if pointScaled:
# Using screen coords
p = self.scaled
pxy = self.currentScale * np.array(pntXY) + self.currentShift
else:
# Using user coords
p = self.points
pxy = np.array(pntXY)
# determine distance for each point
d = np.sqrt(np.add.reduce((p - pxy) ** 2, 1)) # sqrt(dx^2+dy^2)
pntIndex = np.argmin(d)
dist = d[pntIndex]
return [pntIndex,
self.points[pntIndex],
self.scaled[pntIndex] / self._pointSize,
dist]
class PolyLine(PolyPoints):
"""
Creates PolyLine object
:param points: The points that make up the line
:type points: list of ``[x, y]`` values
:param **attr: keyword attributes
=========================== ============= ====================
Keyword and Default Description Type
=========================== ============= ====================
``colour='black'`` Line color :class:`wx.Colour`
``width=1`` Line width float
``style=wx.PENSTYLE_SOLID`` Line style :class:`wx.PenStyle`
``legend=''`` Legend string str
``drawstyle='line'`` see below str
=========================== ============= ====================
================== ==================================================
Draw style Description
================== ==================================================
``'line'`` Draws an straight line between consecutive points
``'steps-pre'`` Draws a line down from point A and then right to
point B
``'steps-post'`` Draws a line right from point A and then down
to point B
``'steps-mid-x'`` Draws a line horizontally to half way between A
and B, then draws a line vertically, then again
horizontally to point B.
``'steps-mid-y'`` Draws a line vertically to half way between A
and B, then draws a line horizonatally, then
again vertically to point B.
*Note: This typically does not look very good*
================== ==================================================
.. warning::
All methods except ``__init__`` are private.
"""
_attributes = {'colour': 'black',
'width': 1,
'style': wx.PENSTYLE_SOLID,
'legend': '',
'drawstyle': 'line',
}
_drawstyles = ("line", "steps-pre", "steps-post",
"steps-mid-x", "steps-mid-y")
def __init__(self, points, **attr):
PolyPoints.__init__(self, points, attr)
def draw(self, dc, printerScale, coord=None):
"""
Draw the lines.
:param dc: The DC to draw on.
:type dc: :class:`wx.DC`
:param printerScale:
:type printerScale: float
:param coord: The legend coordinate?
:type coord: ???
"""
colour = self.attributes['colour']
width = self.attributes['width'] * printerScale * self._pointSize[0]
style = self.attributes['style']
drawstyle = self.attributes['drawstyle']
if not isinstance(colour, wx.Colour):
colour = wx.Colour(colour)
pen = wx.Pen(colour, width, style)
pen.SetCap(wx.CAP_BUTT)
dc.SetPen(pen)
if coord is None:
if len(self.scaled): # bugfix for Mac OS X
for c1, c2 in zip(self.scaled, self.scaled[1:]):
self._path(dc, c1, c2, drawstyle)
else:
dc.DrawLines(coord) # draw legend line
def getSymExtent(self, printerScale):
"""
Get the Width and Height of the symbol.
:param printerScale:
:type printerScale: float
"""
h = self.attributes['width'] * printerScale * self._pointSize[0]
w = 5 * h
return (w, h)
def _path(self, dc, coord1, coord2, drawstyle):
"""
Calculates the path from coord1 to coord 2 along X and Y
:param dc: The DC to draw on.
:type dc: :class:`wx.DC`
:param coord1: The first coordinate in the coord pair
:type coord1: list, length 2: ``[x, y]``
:param coord2: The second coordinate in the coord pair
:type coord2: list, length 2: ``[x, y]``
:param drawstyle: The type of connector to use
:type drawstyle: str
"""
if drawstyle == 'line':
# Straight line between points.
line = [coord1, coord2]
elif drawstyle == 'steps-pre':
# Up/down to next Y, then right to next X
intermediate = [coord1[0], coord2[1]]
line = [coord1, intermediate, coord2]
elif drawstyle == 'steps-post':
# Right to next X, then up/down to Y
intermediate = [coord2[0], coord1[1]]
line = [coord1, intermediate, coord2]
elif drawstyle == 'steps-mid-x':
# need 3 lines between points: right -> up/down -> right
mid_x = ((coord2[0] - coord1[0]) / 2) + coord1[0]
intermediate1 = [mid_x, coord1[1]]
intermediate2 = [mid_x, coord2[1]]
line = [coord1, intermediate1, intermediate2, coord2]
elif drawstyle == 'steps-mid-y':
# need 3 lines between points: up/down -> right -> up/down
mid_y = ((coord2[1] - coord1[1]) / 2) + coord1[1]
intermediate1 = [coord1[0], mid_y]
intermediate2 = [coord2[0], mid_y]
line = [coord1, intermediate1, intermediate2, coord2]
else:
err_txt = "Invalid drawstyle '{}'. Must be one of {}."
raise ValueError(err_txt.format(drawstyle, self._drawstyles))
dc.DrawLines(line)
class PolySpline(PolyLine):
"""
Creates PolySpline object
:param points: The points that make up the spline
:type points: list of ``[x, y]`` values
:param **attr: keyword attributes
=========================== ============= ====================
Keyword and Default Description Type
=========================== ============= ====================
``colour='black'`` Line color :class:`wx.Colour`
``width=1`` Line width float
``style=wx.PENSTYLE_SOLID`` Line style :class:`wx.PenStyle`
``legend=''`` Legend string str
=========================== ============= ====================
.. warning::
All methods except ``__init__`` are private.
"""
_attributes = {'colour': 'black',
'width': 1,
'style': wx.PENSTYLE_SOLID,
'legend': ''}
def __init__(self, points, **attr):
PolyLine.__init__(self, points, **attr)
def draw(self, dc, printerScale, coord=None):
""" Draw the spline """
colour = self.attributes['colour']
width = self.attributes['width'] * printerScale * self._pointSize[0]
style = self.attributes['style']
if not isinstance(colour, wx.Colour):
colour = wx.Colour(colour)
pen = wx.Pen(colour, width, style)
pen.SetCap(wx.CAP_ROUND)
dc.SetPen(pen)
if coord is None:
if len(self.scaled) >= 3:
dc.DrawSpline(self.scaled)
else:
dc.DrawLines(coord) # draw legend line
class PolyMarker(PolyPoints):
"""
Creates a PolyMarker object.
:param points: The marker coordinates.
:type points: list of ``[x, y]`` values
:param **attr: keyword attributes
================================= ============= ====================
Keyword and Default Description Type
================================= ============= ====================
``marker='circle'`` see below str
``size=2`` Marker size float
``colour='black'`` Outline color :class:`wx.Colour`
``width=1`` Outline width float
``style=wx.PENSTYLE_SOLID`` Outline style :class:`wx.PenStyle`
``fillcolour=colour`` fill color :class:`wx.Colour`
``fillstyle=wx.BRUSHSTYLE_SOLID`` fill style :class:`wx.BrushStyle`
``legend=''`` Legend string str
================================= ============= ====================
=================== ==================================
Marker Description
=================== ==================================
``'circle'`` A circle of diameter ``size``
``'dot'`` A dot. Does not have a size.
``'square'`` A square with side length ``size``
``'triangle'`` An upward-pointed triangle
``'triangle_down'`` A downward-pointed triangle
``'cross'`` An "X" shape
``'plus'`` A "+" shape
=================== ==================================
.. warning::
All methods except ``__init__`` are private.
"""
_attributes = {'colour': 'black',
'width': 1,
'size': 2,
'fillcolour': None,
'fillstyle': wx.BRUSHSTYLE_SOLID,
'marker': 'circle',
'legend': ''}
def __init__(self, points, **attr):
PolyPoints.__init__(self, points, attr)
def draw(self, dc, printerScale, coord=None):
""" Draw the points """
colour = self.attributes['colour']
width = self.attributes['width'] * printerScale * self._pointSize[0]
size = self.attributes['size'] * printerScale * self._pointSize[0]
fillcolour = self.attributes['fillcolour']
fillstyle = self.attributes['fillstyle']
marker = self.attributes['marker']
if colour and not isinstance(colour, wx.Colour):
colour = wx.Colour(colour)
if fillcolour and not isinstance(fillcolour, wx.Colour):
fillcolour = wx.Colour(fillcolour)
dc.SetPen(wx.Pen(colour, width))
if fillcolour:
dc.SetBrush(wx.Brush(fillcolour, fillstyle))
else:
dc.SetBrush(wx.Brush(colour, fillstyle))
if coord is None:
if len(self.scaled): # bugfix for Mac OS X
self._drawmarkers(dc, self.scaled, marker, size)
else:
self._drawmarkers(dc, coord, marker, size) # draw legend marker
def getSymExtent(self, printerScale):
"""Width and Height of Marker"""
s = 5 * self.attributes['size'] * printerScale * self._pointSize[0]
return (s, s)
def _drawmarkers(self, dc, coords, marker, size=1):
f = getattr(self, "_{}".format(marker))
f(dc, coords, size)
def _circle(self, dc, coords, size=1):
fact = 2.5 * size
wh = 5.0 * size
rect = np.zeros((len(coords), 4), np.float) + [0.0, 0.0, wh, wh]
rect[:, 0:2] = coords - [fact, fact]
dc.DrawEllipseList(rect.astype(np.int32))
def _dot(self, dc, coords, size=1):
dc.DrawPointList(coords)
def _square(self, dc, coords, size=1):
fact = 2.5 * size
wh = 5.0 * size
rect = np.zeros((len(coords), 4), np.float) + [0.0, 0.0, wh, wh]
rect[:, 0:2] = coords - [fact, fact]
dc.DrawRectangleList(rect.astype(np.int32))
def _triangle(self, dc, coords, size=1):
shape = [(-2.5 * size, 1.44 * size),
(2.5 * size, 1.44 * size), (0.0, -2.88 * size)]
poly = np.repeat(coords, 3, 0)
poly.shape = (len(coords), 3, 2)
poly += shape
dc.DrawPolygonList(poly.astype(np.int32))
def _triangle_down(self, dc, coords, size=1):
shape = [(-2.5 * size, -1.44 * size),
(2.5 * size, -1.44 * size), (0.0, 2.88 * size)]
poly = np.repeat(coords, 3, 0)
poly.shape = (len(coords), 3, 2)
poly += shape
dc.DrawPolygonList(poly.astype(np.int32))
def _cross(self, dc, coords, size=1):
fact = 2.5 * size
for f in [[-fact, -fact, fact, fact], [-fact, fact, fact, -fact]]:
lines = np.concatenate((coords, coords), axis=1) + f
dc.DrawLineList(lines.astype(np.int32))
def _plus(self, dc, coords, size=1):
fact = 2.5 * size
for f in [[-fact, 0, fact, 0], [0, -fact, 0, fact]]:
lines = np.concatenate((coords, coords), axis=1) + f
dc.DrawLineList(lines.astype(np.int32))
class PolyBarsBase(PolyPoints):
"""
Base class for PolyBars and PolyHistogram.
.. warning::
All methods are private.
"""
_attributes = {'edgecolour': 'black',
'edgewidth': 2,
'edgestyle': wx.PENSTYLE_SOLID,
'legend': '',
'fillcolour': 'red',
'fillstyle': wx.BRUSHSTYLE_SOLID,
'barwidth': 1.0
}
def __init__(self, points, attr):
"""
"""
PolyPoints.__init__(self, points, attr)
def _scaleAndShift(self, data, scale=(1, 1), shift=(0, 0)):
"""same as override method, but retuns a value."""
scaled = scale * data + shift
return scaled
def getSymExtent(self, printerScale):
"""Width and Height of Marker"""
h = self.attributes['edgewidth'] * printerScale * self._pointSize[0]
w = 5 * h
return (w, h)
def set_pen_and_brush(self, dc, printerScale):
pencolour = self.attributes['edgecolour']
penwidth = (self.attributes['edgewidth']
* printerScale * self._pointSize[0])
penstyle = self.attributes['edgestyle']
fillcolour = self.attributes['fillcolour']
fillstyle = self.attributes['fillstyle']
if not isinstance(pencolour, wx.Colour):
pencolour = wx.Colour(pencolour)
pen = wx.Pen(pencolour, penwidth, penstyle)
pen.SetCap(wx.CAP_BUTT)
if not isinstance(fillcolour, wx.Colour):
fillcolour = wx.Colour(fillcolour)
brush = wx.Brush(fillcolour, fillstyle)
dc.SetPen(pen)
dc.SetBrush(brush)
def scale_rect(self, rect):
# Scale the points to the plot area
scaled_rect = self._scaleAndShift(rect,
self.currentScale,
self.currentShift)
# Convert to (left, top, width, height) for drawing
wx_rect = [scaled_rect[0][0], # X (left)
scaled_rect[0][1], # Y (top)
scaled_rect[1][0] - scaled_rect[0][0], # Width
scaled_rect[1][1] - scaled_rect[0][1]] # Height
return wx_rect
def draw(self, dc, printerScale, coord=None):
pass
class PolyBars(PolyBarsBase):
"""
Creates a PolyBars object.
:param points: The data to plot.
:type points: sequence of ``(center, height)`` points
:param **attr: keyword attributes
================================= ============= =======================
Keyword and Default Description Type
================================= ============= =======================
``barwidth=1.0`` bar width float or list of floats
``edgecolour='black'`` edge color :class:`wx.Colour`
``edgewidth=1`` edge width float
``edgestyle=wx.PENSTYLE_SOLID`` edge style :class:`wx.PenStyle`
``fillcolour='red'`` fill color :class:`wx.Colour`
``fillstyle=wx.BRUSHSTYLE_SOLID`` fill style :class:`wx.BrushStyle`
``legend=''`` legend string str
================================= ============= =======================
.. important::
If ``barwidth`` is a list of floats:
+ each bar will have a separate width
+ ``len(barwidth)`` must equal ``len(points)``.
.. warning::
All methods except ``__init__`` are private.
"""
def __init__(self, points, **attr):
PolyBarsBase.__init__(self, points, attr)
def calc_rect(self, x, y, w):
""" Calculate the rectangle for plotting. """
return self.scale_rect([[x - w / 2, y], # left, top
[x + w / 2, 0]]) # right, bottom
def draw(self, dc, printerScale, coord=None):
""" Draw the bars """
self.set_pen_and_brush(dc, printerScale)
barwidth = self.attributes['barwidth']
if coord is None:
if isinstance(barwidth, (int, float)):
# use a single width for all bars
pts = ((x, y, barwidth) for x, y in self.points)
elif isinstance(barwidth, (list, tuple)):
# use a separate width for each bar
if len(barwidth) != len(self.points):
err_str = ("Barwidth ({} items) and Points ({} items) do "
"not have the same length!")
err_str = err_str.format(len(barwidth), len(self.points))
raise ValueError(err_str)
pts = ((x, y, w) for (x, y), w in zip(self.points, barwidth))
else:
# invalid attribute type
err_str = ("Invalid type for 'barwidth'. Expected float, "
"int, or list or tuple of (int or float). Got {}.")
raise TypeError(err_str.format(type(barwidth)))
rects = [self.calc_rect(x, y, w) for x, y, w in pts]
dc.DrawRectangleList(rects)
else:
dc.DrawLines(coord) # draw legend line
class PolyHistogram(PolyBarsBase):
"""
Creates a PolyHistogram object.
:param hist: The histogram data.
:type hist: sequence of ``y`` values that define the heights of the bars
:param binspec: The bin specification.
:type binspec: sequence of ``x`` values that define the edges of the bins
:param **attr: keyword attributes
================================= ============= =======================
Keyword and Default Description Type
================================= ============= =======================
``edgecolour='black'`` edge color :class:`wx.Colour`
``edgewidth=3`` edge width float
``edgestyle=wx.PENSTYLE_SOLID`` edge style :class:`wx.PenStyle`
``fillcolour='blue'`` fill color :class:`wx.Colour`
``fillstyle=wx.BRUSHSTYLE_SOLID`` fill style :class:`wx.BrushStyle`
``legend=''`` legend string str
================================= ============= =======================
.. tip::
Use ``np.histogram()`` to easily create your histogram parameters::
hist_data, binspec = np.histogram(data)
hist_plot = PolyHistogram(hist_data, binspec)
.. important::
``len(binspec)`` must equal ``len(hist) + 1``.
.. warning::
All methods except ``__init__`` are private.
"""
def __init__(self, hist, binspec, **attr):
if len(binspec) != len(hist) + 1:
raise ValueError("Len(binspec) must equal len(hist) + 1")
self.hist = hist
self.binspec = binspec
# define the bins and center x locations
self.bins = list(pairwise(self.binspec))
bar_center_x = (pair[0] + (pair[1] - pair[0])/2 for pair in self.bins)
points = list(zip(bar_center_x, self.hist))
PolyBarsBase.__init__(self, points, attr)
def calc_rect(self, y, low, high):
""" Calculate the rectangle for plotting. """
return self.scale_rect([[low, y], # left, top
[high, 0]]) # right, bottom
def draw(self, dc, printerScale, coord=None):
""" Draw the bars """
self.set_pen_and_brush(dc, printerScale)
if coord is None:
rects = [self.calc_rect(y, low, high)
for y, (low, high)
in zip(self.hist, self.bins)]
dc.DrawRectangleList(rects)
else:
dc.DrawLines(coord) # draw legend line
class PolyBoxPlot(PolyPoints):
"""
Creates a PolyBoxPlot object.
:param data: Raw data to create a box plot from.
:type data: sequence of int or float
:param **attr: keyword attributes
================================= ============= =======================
Keyword and Default Description Type
================================= ============= =======================
``colour='black'`` edge color :class:`wx.Colour`
``width=1`` edge width float
``style=wx.PENSTYLE_SOLID`` edge style :class:`wx.PenStyle`
``legend=''`` legend string str
================================= ============= =======================
.. note::
``np.NaN`` and ``np.inf`` values are ignored.
.. admonition:: TODO
+ [ ] Figure out a better way to get multiple box plots side-by-side
(current method is a hack).
+ [ ] change the X axis to some labels.
+ [ ] Change getClosestPoint to only grab box plot items and outlers?
Currently grabs every data point.
+ [ ] Add more customization such as Pens/Brushes, outlier shapes/size,
and box width.
+ [ ] Figure out how I want to handle log-y: log data then calcBP? Or
should I calc the BP first then the plot it on a log scale?
"""
_attributes = {'colour': 'black',
'width': 1,
'style': wx.PENSTYLE_SOLID,
'legend': '',
}
def __init__(self, points, **attr):
# Set various attributes
self.box_width = 0.5
# Determine the X position and create a 1d dataset.
self.xpos = points[0, 0]
points = points[:, 1]
# Calculate the box plot points and the outliers
self._bpdata = self.calcBpData(points)
self._outliers = self.calcOutliers(points)
points = np.concatenate((self._bpdata, self._outliers))
points = np.array([(self.xpos, x) for x in points])
# Create a jitter for the outliers
self.jitter = (0.05 * np.random.random_sample(len(self._outliers))
+ self.xpos - 0.025)
# Init the parent class
PolyPoints.__init__(self, points, attr)
def _clean_data(self, data=None):
"""
Removes NaN and Inf from the data.
"""
if data is None:
data = self.points
# clean out NaN and infinity values.
data = data[~np.isnan(data)]
data = data[~np.isinf(data)]
return data
def boundingBox(self):
"""
Returns bounding box for the plot.
Override method.
"""
xpos = self.xpos
minXY = np.array([xpos - self.box_width / 2, self._bpdata.min * 0.95])
maxXY = np.array([xpos + self.box_width / 2, self._bpdata.max * 1.05])
return minXY, maxXY
def getClosestPoint(self, pntXY, pointScaled=True):
"""
Returns the index of closest point on the curve, pointXY,
scaledXY, distance x, y in user coords.
Override method.
if pointScaled == True, then based on screen coords
if pointScaled == False, then based on user coords
"""
xpos = self.xpos
# combine the outliers with the box plot data
data_to_use = np.concatenate((self._bpdata, self._outliers))
data_to_use = np.array([(xpos, x) for x in data_to_use])
if pointScaled:
# Use screen coords
p = self.scaled
pxy = self.currentScale * np.array(pntXY) + self.currentShift
else:
# Using user coords
p = self._points
pxy = np.array(pntXY)
# determine distnace for each point
d = np.sqrt(np.add.reduce((p - pxy) ** 2, 1)) # sqrt(dx^2+dy^2)
pntIndex = np.argmin(d)
dist = d[pntIndex]
return [pntIndex,
self.points[pntIndex],
self.scaled[pntIndex] / self._pointSize,
dist]
def getSymExtent(self, printerScale):
"""Width and Height of Marker"""
# TODO: does this need to be updated?
h = self.attributes['width'] * printerScale * self._pointSize[0]
w = 5 * h
return (w, h)
def calcBpData(self, data=None):
"""
Box plot points:
Median (50%)
75%
25%
low_whisker = lowest value that's >= (25% - (IQR * 1.5))
high_whisker = highest value that's <= 75% + (IQR * 1.5)
outliers are outside of 1.5 * IQR
Parameters
----------
data : array-like
The data to plot
Returns
-------
bpdata : collections.namedtuple
Descriptive statistics for data:
(min_data, low_whisker, q25, median, q75, high_whisker, max_data)
"""
data = self._clean_data(data)
min_data = float(np.min(data))
max_data = float(np.max(data))
q25 = float(np.percentile(data, 25))
q75 = float(np.percentile(data, 75))
iqr = q75 - q25
low_whisker = float(data[data >= q25 - 1.5 * iqr].min())
high_whisker = float(data[data <= q75 + 1.5 * iqr].max())
median = float(np.median(data))
BPData = namedtuple("bpdata", ("min", "low_whisker", "q25", "median",
"q75", "high_whisker", "max"))
bpdata = BPData(min_data, low_whisker, q25, median,
q75, high_whisker, max_data)
return bpdata
def calcOutliers(self, data=None):
"""
Calculates the outliers. Must be called after calcBpData.
"""
data = self._clean_data(data)
outliers = data
outlier_bool = np.logical_or(outliers > self._bpdata.high_whisker,
outliers < self._bpdata.low_whisker)
outliers = outliers[outlier_bool]
return outliers
def _scaleAndShift(self, data, scale=(1, 1), shift=(0, 0)):
"""same as override method, but retuns a value."""
scaled = scale * data + shift
return scaled
@TempStyle('pen')
def draw(self, dc, printerScale, coord=None):
"""
Draws a box plot on the DC.
Notes
-----
The following draw order is required:
1. First the whisker line
2. Then the IQR box
3. Lasly the median line.
This is because
+ The whiskers are drawn as single line rather than two lines
+ The median line must be visable over the box if the box has a fill.
Other than that, the draw order can be changed.
"""
self._draw_whisker(dc, printerScale)
self._draw_iqr_box(dc, printerScale)
self._draw_median(dc, printerScale) # median after box
self._draw_whisker_ends(dc, printerScale)
self._draw_outliers(dc, printerScale)
@TempStyle('pen')
def _draw_whisker(self, dc, printerScale):
"""Draws the whiskers as a single line"""
xpos = self.xpos
# We draw it as one line and then hide the middle part with
# the IQR rectangle
whisker_line = np.array([[xpos, self._bpdata.low_whisker],
[xpos, self._bpdata.high_whisker]])
whisker_line = self._scaleAndShift(whisker_line,
self.currentScale,
self.currentShift)
whisker_pen = wx.Pen(wx.BLACK, 2, wx.PENSTYLE_SOLID)
whisker_pen.SetCap(wx.CAP_BUTT)
dc.SetPen(whisker_pen)
dc.DrawLines(whisker_line)
@TempStyle('pen')
def _draw_iqr_box(self, dc, printerScale):
"""Draws the Inner Quartile Range box"""
xpos = self.xpos
box_w = self.box_width
iqr_box = [[xpos - box_w / 2, self._bpdata.q75], # left, top
[xpos + box_w / 2, self._bpdata.q25]] # right, bottom
# Scale it to the plot area
iqr_box = self._scaleAndShift(iqr_box,
self.currentScale,
self.currentShift)
# rectangles are drawn (left, top, width, height) so adjust
iqr_box = [iqr_box[0][0], # X (left)
iqr_box[0][1], # Y (top)
iqr_box[1][0] - iqr_box[0][0], # Width
iqr_box[1][1] - iqr_box[0][1]] # Height
box_pen = wx.Pen(wx.BLACK, 3, wx.PENSTYLE_SOLID)
box_brush = wx.Brush(wx.GREEN, wx.BRUSHSTYLE_SOLID)
dc.SetPen(box_pen)
dc.SetBrush(box_brush)
dc.DrawRectangleList([iqr_box])
@TempStyle('pen')
def _draw_median(self, dc, printerScale, coord=None):
"""Draws the median line"""
xpos = self.xpos
median_line = np.array(
[[xpos - self.box_width / 2, self._bpdata.median],
[xpos + self.box_width / 2, self._bpdata.median]]
)
median_line = self._scaleAndShift(median_line,
self.currentScale,
self.currentShift)
median_pen = wx.Pen(wx.BLACK, 4, wx.PENSTYLE_SOLID)
median_pen.SetCap(wx.CAP_BUTT)
dc.SetPen(median_pen)
dc.DrawLines(median_line)
@TempStyle('pen')
def _draw_whisker_ends(self, dc, printerScale):
"""Draws the end caps of the whiskers"""
xpos = self.xpos
fence_top = np.array(
[[xpos - self.box_width * 0.2, self._bpdata.high_whisker],
[xpos + self.box_width * 0.2, self._bpdata.high_whisker]]
)
fence_top = self._scaleAndShift(fence_top,
self.currentScale,
self.currentShift)
fence_bottom = np.array(
[[xpos - self.box_width * 0.2, self._bpdata.low_whisker],
[xpos + self.box_width * 0.2, self._bpdata.low_whisker]]
)
fence_bottom = self._scaleAndShift(fence_bottom,
self.currentScale,
self.currentShift)
fence_pen = wx.Pen(wx.BLACK, 2, wx.PENSTYLE_SOLID)
fence_pen.SetCap(wx.CAP_BUTT)
dc.SetPen(fence_pen)
dc.DrawLines(fence_top)
dc.DrawLines(fence_bottom)
@TempStyle('pen')
def _draw_outliers(self, dc, printerScale):
"""Draws dots for the outliers"""
# Set the pen
outlier_pen = wx.Pen(wx.BLUE, 5, wx.PENSTYLE_SOLID)
dc.SetPen(outlier_pen)
outliers = self._outliers
# Scale the data for plotting
pt_data = np.array([self.jitter, outliers]).T
pt_data = self._scaleAndShift(pt_data,
self.currentScale,
self.currentShift)
# Draw the outliers
size = 0.5
fact = 2.5 * size
wh = 5.0 * size
rect = np.zeros((len(pt_data), 4), np.float) + [0.0, 0.0, wh, wh]
rect[:, 0:2] = pt_data - [fact, fact]
dc.DrawRectangleList(rect.astype(np.int32))
class PlotGraphics(object):
"""
Creates a PlotGraphics object.
:param objects: The Poly objects to plot.
:type objects: list of :class:`~wx.lib.plot.PolyPoints` objects
:param title: The title shown at the top of the graph.
:type title: str
:param xLabel: The x-axis label.
:type xLabel: str
:param yLabel: The y-axis label.
:type yLabel: str
.. warning::
All methods except ``__init__`` are private.
"""
def __init__(self, objects, title='', xLabel='', yLabel=''):
if type(objects) not in [list, tuple]:
raise TypeError("objects argument should be list or tuple")
self.objects = objects
self._title = title
self._xLabel = xLabel
self._yLabel = yLabel
self._pointSize = (1.0, 1.0)
@property
def logScale(self):
if len(self.objects) == 0:
return
return [obj.logScale for obj in self.objects]
@logScale.setter
def logScale(self, logscale):
# XXX: error checking done by PolyPoints class
# if not isinstance(logscale, tuple) and len(logscale) != 2:
# raise TypeError("logscale must be a 2-tuple of bools")
if len(self.objects) == 0:
return
for obj in self.objects:
obj.logScale = logscale
def setLogScale(self, logscale):
"""
Set the log scale boolean value.
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.logScale`
property instead.
"""
pendingDeprecation("self.logScale property")
self.logScale = logscale
@property
def absScale(self):
if len(self.objects) == 0:
return
return [obj.absScale for obj in self.objects]
@absScale.setter
def absScale(self, absscale):
# XXX: error checking done by PolyPoints class
# if not isinstance(absscale, tuple) and len(absscale) != 2:
# raise TypeError("absscale must be a 2-tuple of bools")
if len(self.objects) == 0:
return
for obj in self.objects:
obj.absScale = absscale
def boundingBox(self):
p1, p2 = self.objects[0].boundingBox()
for o in self.objects[1:]:
p1o, p2o = o.boundingBox()
p1 = np.minimum(p1, p1o)
p2 = np.maximum(p2, p2o)
return p1, p2
def scaleAndShift(self, scale=(1, 1), shift=(0, 0)):
for o in self.objects:
o.scaleAndShift(scale, shift)
def setPrinterScale(self, scale):
"""
Thickens up lines and markers only for printing
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.printerScale`
property instead.
"""
pendingDeprecation("self.printerScale property")
self.printerScale = scale
def setXLabel(self, xLabel=''):
"""
Set the X axis label on the graph
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.xLabel`
property instead.
"""
pendingDeprecation("self.xLabel property")
self.xLabel = xLabel
def setYLabel(self, yLabel=''):
"""
Set the Y axis label on the graph
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.yLabel`
property instead.
"""
pendingDeprecation("self.yLabel property")
self.yLabel = yLabel
def setTitle(self, title=''):
"""
Set the title at the top of graph
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.title`
property instead.
"""
pendingDeprecation("self.title property")
self.title = title
def getXLabel(self):
"""
Get X axis label string
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.xLabel`
property instead.
"""
pendingDeprecation("self.xLabel property")
return self.xLabel
def getYLabel(self):
"""
Get Y axis label string
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.yLabel`
property instead.
"""
pendingDeprecation("self.yLabel property")
return self.yLabel
def getTitle(self, title=''):
"""
Get the title at the top of graph
.. deprecated:: Feb 27, 2016
Use the :attr:`~wx.lib.plot.polyobjects.PlotGraphics.title`
property instead.
"""
pendingDeprecation("self.title property")
return self.title
@property
def printerScale(self):
return self._printerScale
@printerScale.setter
def printerScale(self, scale):
"""Thickens up lines and markers only for printing"""
self._printerScale = scale
@property
def xLabel(self):
"""Get the X axis label on the graph"""
return self._xLabel
@xLabel.setter
def xLabel(self, text):
self._xLabel = text
@property
def yLabel(self):
"""Get the Y axis label on the graph"""
return self._yLabel
@yLabel.setter
def yLabel(self, text):
self._yLabel = text
@property
def title(self):
"""Get the title at the top of graph"""
return self._title
@title.setter
def title(self, text):
self._title = text
def draw(self, dc):
for o in self.objects:
# t=_time.perf_counter() # profile info
o._pointSize = self._pointSize
o.draw(dc, self._printerScale)
# print(o, "time=", _time.perf_counter()-t)
def getSymExtent(self, printerScale):
"""Get max width and height of lines and markers symbols for legend"""
self.objects[0]._pointSize = self._pointSize
symExt = self.objects[0].getSymExtent(printerScale)
for o in self.objects[1:]:
o._pointSize = self._pointSize
oSymExt = o.getSymExtent(printerScale)
symExt = np.maximum(symExt, oSymExt)
return symExt
def getLegendNames(self):
"""Returns list of legend names"""
lst = [None] * len(self)
for i in range(len(self)):
lst[i] = self.objects[i].getLegend()
return lst
def __len__(self):
return len(self.objects)
def __getitem__(self, item):
return self.objects[item]
# -------------------------------------------------------------------------
# Used to layout the printer page
class PlotPrintout(wx.Printout):
"""Controls how the plot is made in printing and previewing"""
# Do not change method names in this class,
# we have to override wx.Printout methods here!
def __init__(self, graph):
"""graph is instance of plotCanvas to be printed or previewed"""
wx.Printout.__init__(self)
self.graph = graph
def HasPage(self, page):
if page == 1:
return True
else:
return False
def GetPageInfo(self):
return (1, 1, 1, 1) # disable page numbers
def OnPrintPage(self, page):
dc = self.GetDC() # allows using floats for certain functions
# print("PPI Printer",self.GetPPIPrinter())
# print("PPI Screen", self.GetPPIScreen())
# print("DC GetSize", dc.GetSize())
# print("GetPageSizePixels", self.GetPageSizePixels())
# Note PPIScreen does not give the correct number
# Calulate everything for printer and then scale for preview
PPIPrinter = self.GetPPIPrinter() # printer dots/inch (w,h)
# PPIScreen= self.GetPPIScreen() # screen dots/inch (w,h)
dcSize = dc.GetSize() # DC size
if self.graph._antiAliasingEnabled and not isinstance(dc, wx.GCDC):
try:
dc = wx.GCDC(dc)
except Exception:
pass
else:
if self.graph._hiResEnabled:
# high precision - each logical unit is 1/20 of a point
dc.SetMapMode(wx.MM_TWIPS)
pageSize = self.GetPageSizePixels() # page size in terms of pixcels
clientDcSize = self.graph.GetClientSize()
# find what the margins are (mm)
pgSetupData = self.graph.pageSetupData
margLeftSize, margTopSize = pgSetupData.GetMarginTopLeft()
margRightSize, margBottomSize = pgSetupData.GetMarginBottomRight()
# calculate offset and scale for dc
pixLeft = margLeftSize * PPIPrinter[0] / 25.4 # mm*(dots/in)/(mm/in)
pixRight = margRightSize * PPIPrinter[0] / 25.4
pixTop = margTopSize * PPIPrinter[1] / 25.4
pixBottom = margBottomSize * PPIPrinter[1] / 25.4
plotAreaW = pageSize[0] - (pixLeft + pixRight)
plotAreaH = pageSize[1] - (pixTop + pixBottom)
# ratio offset and scale to screen size if preview
if self.IsPreview():
ratioW = float(dcSize[0]) / pageSize[0]
ratioH = float(dcSize[1]) / pageSize[1]
pixLeft *= ratioW
pixTop *= ratioH
plotAreaW *= ratioW
plotAreaH *= ratioH
# rescale plot to page or preview plot area
self.graph._setSize(plotAreaW, plotAreaH)
# Set offset and scale
dc.SetDeviceOrigin(pixLeft, pixTop)
# Thicken up pens and increase marker size for printing
ratioW = float(plotAreaW) / clientDcSize[0]
ratioH = float(plotAreaH) / clientDcSize[1]
aveScale = (ratioW + ratioH) / 2
if self.graph._antiAliasingEnabled and not self.IsPreview():
scale = dc.GetUserScale()
dc.SetUserScale(scale[0] / self.graph._pointSize[0],
scale[1] / self.graph._pointSize[1])
self.graph._setPrinterScale(aveScale) # tickens up pens for printing
self.graph._printDraw(dc)
# rescale back to original
self.graph._setSize()
self.graph._setPrinterScale(1)
self.graph.Redraw() # to get point label scale and shift correct
return True
|