|
| 1 | +from itertools import chain |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from pyqtgraph.graphicsItems.ScatterPlotItem import ScatterPlotItem |
| 6 | + |
| 7 | + |
| 8 | +def numpy_repr(a): |
| 9 | + """ A numpy repr without summarization """ |
| 10 | + opts = np.get_printoptions() |
| 11 | + try: |
| 12 | + np.set_printoptions(threshold=10**10) |
| 13 | + return repr(a) |
| 14 | + finally: |
| 15 | + np.set_printoptions(**opts) |
| 16 | + |
| 17 | + |
| 18 | +def scatterplot_code(scatterplot_item): |
| 19 | + x = scatterplot_item.data['x'] |
| 20 | + y = scatterplot_item.data['y'] |
| 21 | + sizes = scatterplot_item.data["size"] |
| 22 | + |
| 23 | + code = [] |
| 24 | + |
| 25 | + code.append("# data") |
| 26 | + code.append("x = {}".format(numpy_repr(x))) |
| 27 | + code.append("y = {}".format(numpy_repr(y))) |
| 28 | + |
| 29 | + code.append("# style") |
| 30 | + code.append("sizes = {}".format(numpy_repr(sizes))) |
| 31 | + |
| 32 | + def colortuple(color): |
| 33 | + return color.redF(), color.greenF(), color.blueF(), color.alphaF() |
| 34 | + |
| 35 | + edgecolors = np.array([colortuple(a.color()) for a in scatterplot_item.data["pen"]]) |
| 36 | + facecolors = np.array([colortuple(a.color()) for a in scatterplot_item.data["brush"]]) |
| 37 | + |
| 38 | + code.append("edgecolors = {}".format(numpy_repr(edgecolors))) |
| 39 | + code.append("facecolors = {}".format(numpy_repr(facecolors))) |
| 40 | + |
| 41 | + # possible_markers for scatterplot are in .graph.CurveSymbols |
| 42 | + def matplotlib_marker(m): |
| 43 | + if m == "t": |
| 44 | + return "^" |
| 45 | + elif m == "t2": |
| 46 | + return ">" |
| 47 | + elif m == "t3": |
| 48 | + return "<" |
| 49 | + elif m == "star": |
| 50 | + return "*" |
| 51 | + elif m == "+": |
| 52 | + return "P" |
| 53 | + elif m == "x": |
| 54 | + return "X" |
| 55 | + return m |
| 56 | + |
| 57 | + # TODO labels are missing |
| 58 | + |
| 59 | + # each marker requires one call to matplotlib's scatter! |
| 60 | + markers = np.array([matplotlib_marker(m) for m in scatterplot_item.data["symbol"]]) |
| 61 | + for m in set(markers): |
| 62 | + indices = np.where(markers == m)[0] |
| 63 | + if np.all(indices == np.arange(x.shape[0])): |
| 64 | + # indices are unused |
| 65 | + code.append("plt.scatter(x=x, y=y, s=sizes**2/4, marker={},".format(repr(m))) |
| 66 | + code.append(" facecolors=facecolors, edgecolors=edgecolors)") |
| 67 | + else: |
| 68 | + code.append("indices = {}".format(numpy_repr(indices))) |
| 69 | + code.append("plt.scatter(x=x[indices], y=y[indices], s=sizes[indices]**2/4, " |
| 70 | + "marker={},".format(repr(m))) |
| 71 | + code.append(" facecolors=facecolors[indices], " |
| 72 | + "edgecolors=edgecolors[indices])") |
| 73 | + |
| 74 | + return "\n".join(code) |
| 75 | + |
| 76 | + |
| 77 | +def scene_code(scene): |
| 78 | + |
| 79 | + code = [] |
| 80 | + |
| 81 | + code.append("import matplotlib.pyplot as plt") |
| 82 | + code.append("from numpy import array") |
| 83 | + |
| 84 | + code.append("") |
| 85 | + code.append("plt.clf()") |
| 86 | + |
| 87 | + code.append("") |
| 88 | + |
| 89 | + for item in scene.items: |
| 90 | + if isinstance(item, ScatterPlotItem): |
| 91 | + code.append(scatterplot_code(item)) |
| 92 | + |
| 93 | + # TODO currently does not work for graphs without axes and for multiple axes! |
| 94 | + for position, set_ticks, set_label in [("bottom", "plt.xticks", "plt.xlabel"), |
| 95 | + ("left", "plt.yticks", "plt.ylabel")]: |
| 96 | + axis = scene.getAxis(position) |
| 97 | + code.append("{}({})".format(set_label, repr(str(axis.labelText)))) |
| 98 | + |
| 99 | + # textual tick labels |
| 100 | + if axis._tickLevels is not None: |
| 101 | + major_minor = list(chain(*axis._tickLevels)) |
| 102 | + locs = [a[0] for a in major_minor] |
| 103 | + labels = [a[1] for a in major_minor] |
| 104 | + code.append("{}({}, {})".format(set_ticks, locs, repr(labels))) |
| 105 | + |
| 106 | + return "\n".join(code) |
| 107 | + |
| 108 | + |
0 commit comments