(dataToPlot,
clusterLabels=None,
clusterLabelPosition=(0.1, -0.05),
categoryLabelPosition=(1.0, -0.05),
title="multiple stacked bar plot",
H="/",
**kwargs)
| 146 | |
| 147 | # Taken from https://stackoverflow.com/questions/22787209/how-to-have-clusters-of-stacked-bars |
| 148 | def plotClusteredStackedBarchart(dataToPlot, |
| 149 | clusterLabels=None, |
| 150 | clusterLabelPosition=(0.1, -0.05), |
| 151 | categoryLabelPosition=(1.0, -0.05), |
| 152 | title="multiple stacked bar plot", |
| 153 | H="/", |
| 154 | **kwargs): |
| 155 | n_df = len(dataToPlot) |
| 156 | n_col = len(dataToPlot[0].columns) |
| 157 | n_ind = len(dataToPlot[0].index) |
| 158 | axe = plt.subplot(111) |
| 159 | |
| 160 | for df in dataToPlot: # for each data frame |
| 161 | axe = df.plot(kind="bar", |
| 162 | linewidth=0, |
| 163 | stacked=True, |
| 164 | ax=axe, |
| 165 | legend=False, |
| 166 | grid=False, |
| 167 | **kwargs) # make bar plots |
| 168 | |
| 169 | subtractFromXOffset = 0 |
| 170 | if n_df <= 2: |
| 171 | subtractFromXOffset = 0.10 |
| 172 | elif n_df <= 4: |
| 173 | subtractFromXOffset = 0.15 |
| 174 | elif n_df <= 8: |
| 175 | subtractFromXOffset = 0.2 |
| 176 | elif n_df <= 16: |
| 177 | subtractFromXOffset = 0.22 |
| 178 | H = '' |
| 179 | else: |
| 180 | subtractFromXOffset = 0.23 |
| 181 | H = '' |
| 182 | |
| 183 | h, l = axe.get_legend_handles_labels() # get the handles we want to modify |
| 184 | for i in range(0, n_df * n_col, n_col): # len(h) = n_col * n_df |
| 185 | for j, pa in enumerate(h[i:i + n_col]): |
| 186 | for rect in pa.patches: # for each index |
| 187 | rect.set_x(rect.get_x() + 1 / float(n_df + 1) * i / float( |
| 188 | n_col) - subtractFromXOffset) # for 8 clusters subtract 0.15 |
| 189 | rect.set_hatch(2 * H * int(i / n_col)) # edited part |
| 190 | rect.set_width(1 / float(n_df + 1)) |
| 191 | |
| 192 | axe.set_xticks(-0.18 + (np.arange(0, 2 * n_ind, 2) + 1 / float(n_df + 1)) / 2.) |
| 193 | axe.set_xticklabels(df.index, rotation=0) |
| 194 | axe.set_title(title) |
| 195 | |
| 196 | # Add invisible data to add another legend |
| 197 | n = [] |
| 198 | for i in range(n_df): |
| 199 | n.append(axe.bar(0, 0, color="gray", hatch=H * i)) |
| 200 | |
| 201 | # if categoryLabelPosition is not None: |
| 202 | # l1 = axe.legend(h[:n_col], l[:n_col], ncol=1, bbox_to_anchor=categoryLabelPosition) |
| 203 | # axe.add_artist(l1) |
| 204 | |
| 205 | if clusterLabels is not None and n_df <= 8 and clusterLabelPosition is not None: |
no outgoing calls
no test coverage detected