|
2 | 2 |
|
3 | 3 | import numpy as np |
4 | 4 |
|
| 5 | +from matplotlib.colors import to_hex |
5 | 6 | from pyqtgraph.graphicsItems.ScatterPlotItem import ScatterPlotItem |
6 | 7 | from AnyQt.QtCore import Qt |
7 | 8 | from AnyQt.QtGui import QPen, QBrush |
@@ -40,6 +41,18 @@ def code_with_indices(data, data_name, indices, indices_name): |
40 | 41 | return data_name |
41 | 42 |
|
42 | 43 |
|
| 44 | +def index_per_different(l): |
| 45 | + different = [] |
| 46 | + different_ind = {} |
| 47 | + index = [] |
| 48 | + for e in l: |
| 49 | + if e not in different_ind: |
| 50 | + different_ind[e] = len(different) |
| 51 | + different.append(e) |
| 52 | + index.append(different_ind[e]) |
| 53 | + return different, index |
| 54 | + |
| 55 | + |
43 | 56 | def scatterplot_code(scatterplot_item): |
44 | 57 | x = scatterplot_item.data['x'] |
45 | 58 | y = scatterplot_item.data['y'] |
@@ -84,23 +97,34 @@ def shown(a): |
84 | 97 | if not any(shown_edge) and not any(shown_brush): |
85 | 98 | return "" |
86 | 99 |
|
87 | | - def do_colors(data_column, show): |
| 100 | + def do_colors(code, data_column, show, name): |
88 | 101 | colors = [colortuple(a) for a in data_column] |
89 | 102 | if all(a is None for a in colors): |
90 | | - colors = None |
| 103 | + colors, index = None, None |
91 | 104 | else: |
92 | 105 | # replace None values with blue colors |
93 | 106 | colors = np.array([((0, 0, 1, 1) if a is None else a) |
94 | 107 | for a in colors]) |
95 | 108 | # set alpha for hidden (Qt.NoPen, Qt.NoBrush) elements to zero |
96 | 109 | colors[:, 3][np.array(show) == 0] = 0 |
97 | | - return colors |
| 110 | + # shorter color names for printout |
| 111 | + colors = [to_hex(c, keep_alpha=True) for c in colors] |
| 112 | + colors, index = index_per_different(colors) |
| 113 | + |
| 114 | + code.append("{} = {}".format(name, repr(colors))) |
| 115 | + if index is not None: |
| 116 | + code.append("{}_index = {}".format(name, repr(index))) |
| 117 | + |
| 118 | + decompresssed_code = name |
| 119 | + if index is not None: |
| 120 | + decompresssed_code = "array({})[{}_index]".format(name, name) |
| 121 | + colors = np.array(colors)[index] |
| 122 | + |
| 123 | + return colors, decompresssed_code |
98 | 124 |
|
99 | | - edgecolors = do_colors(scatterplot_item.data["pen"], shown_edge) |
100 | | - facecolors = do_colors(scatterplot_item.data["brush"], shown_brush) |
| 125 | + edgecolors, edgecolors_code = do_colors(code, scatterplot_item.data["pen"], shown_edge, "edgecolors") |
| 126 | + facecolors, facecolors_code = do_colors(code, scatterplot_item.data["brush"], shown_brush, "facecolors") |
101 | 127 |
|
102 | | - code.append("edgecolors = {}".format(numpy_repr(edgecolors))) |
103 | | - code.append("facecolors = {}".format(numpy_repr(facecolors))) |
104 | 128 | linewidths = compress_if_all_same(linewidths) |
105 | 129 | code.append("linewidths = {}".format(numpy_repr(linewidths))) |
106 | 130 |
|
@@ -141,8 +165,8 @@ def indexed(data, data_name, indices=indices): |
141 | 165 | indexed(y, "y"), |
142 | 166 | (indexed(sizes, "sizes") + "**2/4") if sizes is not None else "sizes", |
143 | 167 | repr(m), |
144 | | - indexed(facecolors, "facecolors"), |
145 | | - indexed(edgecolors, "edgecolors"), |
| 168 | + indexed(facecolors, facecolors_code), |
| 169 | + indexed(edgecolors, edgecolors_code), |
146 | 170 | indexed(linewidths, "linewidths") |
147 | 171 | )) |
148 | 172 |
|
|
0 commit comments