Skip to content

Commit ee6379d

Browse files
committed
Support varying colors in a PatchCollection
1 parent 9d86cfc commit ee6379d

File tree

3 files changed

+47
-50
lines changed

3 files changed

+47
-50
lines changed

test/test_patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def plot():
5555
patches.append(polygon)
5656

5757
colors = 100 * np.random.rand(len(patches))
58-
p = PatchCollection(patches, cmap=mpl.cm.jet, alpha=0.4)
58+
p = PatchCollection(patches, cmap=mpl.cm.viridis, alpha=0.4)
5959
p.set_array(np.array(colors))
6060
ax.add_collection(p)
6161

test/test_patches_reference.tex

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
\begin{tikzpicture}
22

3-
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
3+
\definecolor{color0}{rgb}{0.241237,0.296485,0.539709}
4+
\definecolor{color1}{rgb}{0.187231,0.414746,0.556547}
5+
\definecolor{color2}{rgb}{0.636902,0.856542,0.21662}
6+
\definecolor{color3}{rgb}{0.273006,0.20452,0.501721}
7+
\definecolor{color4}{rgb}{0.15627,0.489624,0.557936}
8+
\definecolor{color5}{rgb}{0.993248,0.906157,0.143936}
9+
\definecolor{color6}{rgb}{0.140536,0.530132,0.555659}
10+
\definecolor{color7}{rgb}{0.12138,0.629492,0.531973}
11+
\definecolor{color8}{rgb}{0.267004,0.004874,0.329415}
12+
\definecolor{color9}{rgb}{0.515992,0.831158,0.294279}
13+
\definecolor{color10}{rgb}{0.119699,0.61849,0.536347}
14+
\definecolor{color11}{rgb}{0.131172,0.555899,0.552459}
15+
\definecolor{color12}{rgb}{0.227802,0.326594,0.546532}
16+
\definecolor{color13}{rgb}{0.248629,0.278775,0.534556}
17+
\definecolor{color14}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
418

519
\begin{axis}[
620
colorbar,
721
colorbar style={ylabel={}},
8-
colormap={mymap}{[1pt]
9-
rgb(0pt)=(0,0,0.5);
10-
rgb(22pt)=(0,0,1);
11-
rgb(25pt)=(0,0,1);
12-
rgb(68pt)=(0,0.86,1);
13-
rgb(70pt)=(0,0.9,0.967741935483871);
14-
rgb(75pt)=(0.0806451612903226,1,0.887096774193548);
15-
rgb(128pt)=(0.935483870967742,1,0.0322580645161291);
16-
rgb(130pt)=(0.967741935483871,0.962962962962963,0);
17-
rgb(132pt)=(1,0.925925925925926,0);
18-
rgb(178pt)=(1,0.0740740740740741,0);
19-
rgb(182pt)=(0.909090909090909,0,0);
20-
rgb(200pt)=(0.5,0,0)
21-
},
22+
colormap/viridis,
2223
point meta max=98.5559785610705,
2324
point meta min=12.0628665990324,
2425
tick align=outside,
@@ -41,7 +42,7 @@
4142
.. controls (axis cs:0.5983927657594,0.52530459850483) and (axis cs:0.60872671606002,0.500356235536182) .. (axis cs:0.627118684055587,0.481964267540616)
4243
.. controls (axis cs:0.645510652051153,0.46357229954505) and (axis cs:0.6704590150198,0.45323834924443) .. (axis cs:0.696469185597862,0.45323834924443)
4344
--cycle;
44-
\path [fill=color0, fill opacity=0.4]
45+
\path [fill=color1, fill opacity=0.4]
4546
(axis cs:0.286139334950379,0.650985995927077)
4647
.. controls (axis cs:0.304301231914869,0.650985995927077) and (axis cs:0.321721709658734,0.658201794071604) .. (axis cs:0.334564110161536,0.671044194574406)
4748
.. controls (axis cs:0.347406510664338,0.683886595077208) and (axis cs:0.354622308808866,0.701307072821074) .. (axis cs:0.354622308808866,0.719468969785563)
@@ -52,7 +53,7 @@
5253
.. controls (axis cs:0.217656361091893,0.701307072821074) and (axis cs:0.224872159236421,0.683886595077208) .. (axis cs:0.237714559739223,0.671044194574406)
5354
.. controls (axis cs:0.250556960242025,0.658201794071604) and (axis cs:0.26797743798589,0.650985995927077) .. (axis cs:0.286139334950379,0.650985995927077)
5455
--cycle;
55-
\path [fill=color0, fill opacity=0.4]
56+
\path [fill=color2, fill opacity=0.4]
5657
(axis cs:0.226851453564203,0.375013269976025)
5758
.. controls (axis cs:0.239605916680458,0.375013269976025) and (axis cs:0.251839707087159,0.38008067188171) .. (axis cs:0.260858474447056,0.389099439241608)
5859
.. controls (axis cs:0.269877241806954,0.398118206601505) and (axis cs:0.274944643712639,0.410351997008206) .. (axis cs:0.274944643712639,0.423106460124461)
@@ -63,13 +64,13 @@
6364
.. controls (axis cs:0.178758263415767,0.410351997008206) and (axis cs:0.183825665321452,0.398118206601505) .. (axis cs:0.19284443268135,0.389099439241608)
6465
.. controls (axis cs:0.201863200041247,0.38008067188171) and (axis cs:0.214096990447948,0.375013269976025) .. (axis cs:0.226851453564203,0.375013269976025)
6566
--cycle;
66-
\path [fill=color0, fill opacity=0.4]
67+
\path [fill=color3, fill opacity=0.4]
6768
(axis cs:0,0.25)
6869
--(axis cs:0.707106781186548,-0.457106781186547)
6970
--(axis cs:1.06066017177982,-0.103553390593274)
7071
--(axis cs:0.353553390593274,0.603553390593274)
7172
--cycle;
72-
\path [fill=color0, fill opacity=0.4]
73+
\path [fill=color4, fill opacity=0.4]
7374
(axis cs:0.319763407924357,0.424037633715361)
7475
.. controls (axis cs:0.322195069846986,0.41193268399766) and (axis cs:0.32762605917823,0.400629304754155) .. (axis cs:0.335557083587924,0.39116666034644)
7576
.. controls (axis cs:0.343488107997617,0.381704015938725) and (axis cs:0.353668440565538,0.374381253055418) .. (axis cs:0.365162163052912,0.369871582251773)
@@ -78,14 +79,14 @@
7879
--(axis cs:0.392117518194151,0.438572244679624)
7980
--(axis cs:0.319763407924357,0.424037633715361)
8081
--cycle;
81-
\path [fill=color0, fill opacity=0.4]
82+
\path [fill=color5, fill opacity=0.4]
8283
(axis cs:0.325292534093276,0.0560527278677551)
8384
.. controls (axis cs:0.326029332980179,0.0524175858342907) and (axis cs:0.327857149748153,0.0490920334084667) .. (axis cs:0.330531105175883,0.0465216068731004)
8485
.. controls (axis cs:0.333205060603613,0.0439511803377341) and (axis cs:0.336600159513475,0.0422560353685811) .. (axis cs:0.340261548228723,0.0416632766663606)
8586
--(axis cs:0.343178016150869,0.0596778966095683)
8687
--(axis cs:0.325292534093276,0.0560527278677551)
8788
--cycle;
88-
\path [fill=color0, fill opacity=0.4]
89+
\path [fill=color6, fill opacity=0.4]
8990
(axis cs:0.717397420978084,0.384927189771934)
9091
.. controls (axis cs:0.719079422931577,0.383433016520297) and (axis cs:0.721033228508406,0.382276220003659) .. (axis cs:0.723152137857449,0.381519974376895)
9192
.. controls (axis cs:0.725271047206493,0.380763728750131) and (axis cs:0.727515773023645,0.380422055779642) .. (axis cs:0.729763727926958,0.380513614668531)
@@ -106,7 +107,7 @@
106107
--(axis cs:0.729049707384042,0.398044255330431)
107108
--(axis cs:0.717397420978084,0.384927189771934)
108109
--cycle;
109-
\path [fill=color0, fill opacity=0.4]
110+
\path [fill=color7, fill opacity=0.4]
110111
(axis cs:0.4,0.7)
111112
.. controls (axis cs:0.4,0.713131879680739) and (axis cs:0.397413306040759,0.726136068376074) .. (axis cs:0.392387953251129,0.738268343236509)
112113
.. controls (axis cs:0.387362600461498,0.750400618096944) and (axis cs:0.379996319290631,0.761425036946679) .. (axis cs:0.370710678118655,0.770710678118655)
@@ -127,7 +128,7 @@
127128
(axis cs:0.3,0.7)
128129
(axis cs:0.4,0.7)
129130
--cycle;
130-
\path [fill=color0, fill opacity=0.4]
131+
\path [fill=color8, fill opacity=0.4]
131132
(axis cs:0.9,0.8)
132133
.. controls (axis cs:0.9,0.826263759361477) and (axis cs:0.894826612081518,0.852272136752148) .. (axis cs:0.884775906502257,0.876536686473018)
133134
.. controls (axis cs:0.874725200922996,0.900801236193888) and (axis cs:0.859992638581262,0.922850073893357) .. (axis cs:0.84142135623731,0.941421356237309)
@@ -164,14 +165,14 @@
164165
.. controls (axis cs:0.846119959061139,0.839204102564111) and (axis cs:0.85,0.819697819521108) .. (axis cs:0.85,0.8)
165166
(axis cs:0.9,0.8)
166167
--cycle;
167-
\path [fill=color0, fill opacity=0.4]
168+
\path [fill=color9, fill opacity=0.4]
168169
(axis cs:1,0.3)
169170
.. controls (axis cs:1,0.326263759361477) and (axis cs:0.994826612081518,0.352272136752148) .. (axis cs:0.984775906502257,0.376536686473018)
170171
.. controls (axis cs:0.974725200922997,0.400801236193888) and (axis cs:0.959992638581262,0.422850073893357) .. (axis cs:0.94142135623731,0.441421356237309)
171172
--(axis cs:0.8,0.3)
172173
--(axis cs:1,0.3)
173174
--cycle;
174-
\path [fill=color0, fill opacity=0.4]
175+
\path [fill=color10, fill opacity=0.4]
175176
(axis cs:0.94142135623731,0.441421356237309)
176177
.. controls (axis cs:0.922850073893357,0.459992638581262) and (axis cs:0.900801236193888,0.474725200922997) .. (axis cs:0.876536686473018,0.484775906502257)
177178
.. controls (axis cs:0.852272136752148,0.494826612081518) and (axis cs:0.826263759361477,0.5) .. (axis cs:0.8,0.5)
@@ -180,23 +181,23 @@
180181
.. controls (axis cs:0.850400618096944,0.387362600461498) and (axis cs:0.861425036946679,0.379996319290631) .. (axis cs:0.870710678118655,0.370710678118655)
181182
--(axis cs:0.94142135623731,0.441421356237309)
182183
--cycle;
183-
\path [fill=color0, fill opacity=0.4]
184+
\path [fill=color11, fill opacity=0.4]
184185
(axis cs:0.722443382570222,0.322958913853178)
185186
--(axis cs:0.361788655622314,0.228263230878956)
186187
--(axis cs:0.293714046388829,0.630976123854488)
187188
--cycle;
188-
\path [fill=color0, fill opacity=0.4]
189+
\path [fill=color12, fill opacity=0.4]
189190
(axis cs:0.0921049399450752,0.433701172679528)
190191
--(axis cs:0.430862763329644,0.493685097650306)
191192
--(axis cs:0.425830290295828,0.312261222972465)
192193
--cycle;
193-
\path [fill=color0, fill opacity=0.4]
194+
\path [fill=color13, fill opacity=0.4]
194195
(axis cs:0.426351306962808,0.893389163117135)
195196
--(axis cs:0.94416001820388,0.501836675884337)
196197
--(axis cs:0.623952951792111,0.115618395079296)
197198
--cycle;
198199

199-
\draw[draw=none,fill=color0,fill opacity=0.4,rotate around={45:(axis cs:1,0.5)}] (axis cs:1,0.5) ellipse (0.5 and 0.25);
200+
\draw[draw=none,fill=color14,fill opacity=0.4,rotate around={45:(axis cs:1,0.5)}] (axis cs:1,0.5) ellipse (0.5 and 0.25);
200201
\draw[draw=red,fill=red,opacity=0.4] (axis cs:0,1) circle (0.5);
201202
\draw[->,draw=black] (axis cs:0.25,0.25) -- (axis cs:0.5,0.25);
202203
\path [draw=black, -latex, draw=black]

tikzplotlib/_patch.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,27 @@ def draw_patchcollection(data, obj):
6767
"""Returns PGFPlots code for a number of patch objects.
6868
"""
6969
content = []
70-
# Gather the draw options.
71-
try:
72-
ec = obj.get_edgecolor()[0]
73-
except IndexError:
74-
ec = None
75-
76-
try:
77-
fc = obj.get_facecolor()[0]
78-
except IndexError:
79-
fc = None
8070

81-
try:
82-
ls = obj.get_linestyle()[0]
83-
except IndexError:
84-
ls = None
71+
# recompute the face colors
72+
obj.update_scalarmappable()
8573

86-
try:
87-
w = obj.get_linewidth()[0]
88-
except IndexError:
89-
w = None
74+
def ensure_list(x):
75+
return [None] if len(x) == 0 else x
9076

91-
data, draw_options = mypath.get_draw_options(data, obj, ec, fc, ls, w)
77+
ecs = ensure_list(obj.get_edgecolor())
78+
fcs = ensure_list(obj.get_facecolor())
79+
lss = ensure_list(obj.get_linestyle())
80+
ws = ensure_list(obj.get_linewidth())
9281

9382
paths = obj.get_paths()
94-
for path in paths:
83+
for i, path in enumerate(paths):
84+
# Gather the draw options.
85+
ec = ecs[i % len(ecs)]
86+
fc = fcs[i % len(fcs)]
87+
ls = lss[i % len(lss)]
88+
w = ws[i % len(ws)]
89+
90+
data, draw_options = mypath.get_draw_options(data, obj, ec, fc, ls, w)
9591
data, cont, draw_options, is_area = mypath.draw_path(
9692
data, path, draw_options=draw_options
9793
)

0 commit comments

Comments
 (0)