Skip to content

Commit c19e777

Browse files
committed
Matplotlib export: compress colors
1 parent b064dc2 commit c19e777

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

Orange/widgets/utils/matplotlib_export.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44

5+
from matplotlib.colors import to_hex
56
from pyqtgraph.graphicsItems.ScatterPlotItem import ScatterPlotItem
67
from AnyQt.QtCore import Qt
78
from AnyQt.QtGui import QPen, QBrush
@@ -40,6 +41,18 @@ def code_with_indices(data, data_name, indices, indices_name):
4041
return data_name
4142

4243

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+
4356
def scatterplot_code(scatterplot_item):
4457
x = scatterplot_item.data['x']
4558
y = scatterplot_item.data['y']
@@ -84,23 +97,34 @@ def shown(a):
8497
if not any(shown_edge) and not any(shown_brush):
8598
return ""
8699

87-
def do_colors(data_column, show):
100+
def do_colors(code, data_column, show, name):
88101
colors = [colortuple(a) for a in data_column]
89102
if all(a is None for a in colors):
90-
colors = None
103+
colors, index = None, None
91104
else:
92105
# replace None values with blue colors
93106
colors = np.array([((0, 0, 1, 1) if a is None else a)
94107
for a in colors])
95108
# set alpha for hidden (Qt.NoPen, Qt.NoBrush) elements to zero
96109
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
98124

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")
101127

102-
code.append("edgecolors = {}".format(numpy_repr(edgecolors)))
103-
code.append("facecolors = {}".format(numpy_repr(facecolors)))
104128
linewidths = compress_if_all_same(linewidths)
105129
code.append("linewidths = {}".format(numpy_repr(linewidths)))
106130

@@ -141,8 +165,8 @@ def indexed(data, data_name, indices=indices):
141165
indexed(y, "y"),
142166
(indexed(sizes, "sizes") + "**2/4") if sizes is not None else "sizes",
143167
repr(m),
144-
indexed(facecolors, "facecolors"),
145-
indexed(edgecolors, "edgecolors"),
168+
indexed(facecolors, facecolors_code),
169+
indexed(edgecolors, edgecolors_code),
146170
indexed(linewidths, "linewidths")
147171
))
148172

0 commit comments

Comments
 (0)