Skip to content

Commit 575b174

Browse files
committed
Fixed plot representation for a a gobsmacking number K values. Close #69
1 parent 4f4d5ef commit 575b174

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

structure_threader/plotter/structplot.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -653,16 +653,19 @@ def plotk(self, kvals, output_dir):
653653
kvals))
654654
raise SystemExit(1)
655655

656+
# Adapt vspace to number of k
657+
vspace = .25 / len(kvals)
658+
656659
# Set the figure object with the subplots and their titles already
657660
# specified
658661
fig = tools.make_subplots(
659662
rows=nplots,
660663
cols=1,
661664
shared_xaxes=True,
662-
subplot_titles=sorted(
663-
[basename(self.kvals[k].file_path) for k in kvals
664-
if k in self.kvals], reverse=True),
665-
vertical_spacing=0.05,
665+
subplot_titles=[
666+
basename(self.kvals[k].file_path) for k in
667+
sorted(kvals, reverse=True) if k in self.kvals],
668+
vertical_spacing=vspace,
666669
print_grid=False)
667670

668671
shape_list = []
@@ -690,8 +693,16 @@ def plotk(self, kvals, output_dir):
690693
qvals = kobj.qvals.T
691694

692695
# Iterate over each meanQ column (corresponding to each cluster)
696+
counter = 0
693697
for p, i in enumerate(qvals):
694698

699+
try:
700+
clr = c[counter]
701+
counter += 1
702+
except IndexError:
703+
counter = 0
704+
clr = c[counter]
705+
695706
# Create Bar trace for each cluster
696707
current_bar = go.Bar(
697708
# Set xticks
@@ -708,8 +719,7 @@ def plotk(self, kvals, output_dir):
708719
text=["Assignment: {}%".format(x * 100) for x in i],
709720
# Customization of bars
710721
marker=dict(
711-
# TODO: Only 12 colors supported for now
712-
color=c[p],
722+
color=clr,
713723
line=dict(
714724
color='grey',
715725
width=2,
@@ -790,10 +800,21 @@ def plotk(self, kvals, output_dir):
790800
fig["layout"].update(shapes=shape_list) # Update first xaxis
791801
fig["layout"]["xaxis1"].update(**xdata)
792802

803+
# Adapt figure size to number of K. Use auto-size for less
804+
# than 3 K.
805+
if len(kvals) > 3:
806+
height = 250 * len(kvals)
807+
size = {"autosize": False,
808+
"width": 1800,
809+
"height": height}
810+
else:
811+
size = {"autosize": True}
812+
793813
fig["layout"].update(barmode="stack",
794814
bargap=0,
795815
margin={"b": bmargin},
796-
legend={"x": 1, "y": 0.5})
816+
legend={"x": 1, "y": 0.5},
817+
**size)
797818

798819
# Determine file name. If a single K value is provided, then
799820
# adapt from the ouptut name of that K value file.
@@ -848,6 +869,7 @@ def plotk_static(self, kval, output_dir, bw=False, use_ind=False):
848869
else:
849870
qvalues = self.kvals[kval].qvals
850871

872+
counter = 0
851873
for i in range(kval):
852874

853875
# Determine color/pattern arguments
@@ -858,9 +880,11 @@ def plotk_static(self, kval, output_dir, bw=False, use_ind=False):
858880
# Get bar color. If K exceeds the 12 colors, generate random
859881
# color
860882
try:
861-
clr = clist[i]
883+
clr = clist[counter]
884+
counter += 1
862885
except IndexError:
863-
clr = np.random.rand(3, 1)
886+
counter = 0
887+
clr = clist[counter]
864888

865889
kwargs["facecolor"] = clr
866890
kwargs["edgecolor"] = "grey"

0 commit comments

Comments
 (0)