How to break line ax.legend

Viewed 89

I want to change the legend of my graph, i wish to add a line break after each labels on the legend of my graph. I have two dict, one with values dans one with colors associate to the labels, that I can't interact with.

dico_valeur = {'A': [5, 2, 2, 6, 2, 0, 0, 1],
 'B': [6, 5, 0, 0, 0, 0, 1, 0],
 'C': [6, 22, 2, 5, 3, 1, 2, 1],
 'D': [13, 3, 2, 1, 3, 0, 0, 0],
 'E': [14, 6, 12, 1, 4, 2, 2, 0],
 'F': [3, 3, 0, 0, 0, 1, 0, 0],
 'G': [6, 11, 3, 6, 1, 0, 1, 1],
 'H': [3, 2, 2, 1, 3, 0, 0, 1],
 'I': [2, 0, 0, 0, 0, 1, 0, 0],
 'J': [6, 10, 1, 5, 3, 1, 0, 0],
 'K': [2, 4, 1, 1, 0, 0, 0, 0],
 'L': [1, 0, 0, 0, 0, 0, 0, 0],
 'M': [1, 0, 0, 0, 0, 0, 0, 0],
 'N': [10, 7, 2, 3, 3, 3, 0, 0]}

dico_couleur = {"a very very very very very very very very very very very very very very very very very very very very long legend": 'C0',
 'another very very very very very very very very very very very very very very very very very very very very long legend, démarreur,…': 'C1',
 'a very very very very very very very very very very very very very very very very very long legend': 'C2',
 "a very very very very very very very very very very very very very very very vry very very very long legend": 'C3',
 'a very very very very very veryy very very very very very very long legend': 'C4',
 'a very very very very very very very very very very very very very very very very very very very very long legend': 'C5',
 "a very very very very very very very very very very very very very very very very very very long legend": 'C6',
 'a very very very very very very very very veryery very very very very very very very very long legend': 'C7'}

for i in dico_couleur: category_names.append(i)
results = dico_valeur
labels = list(results.keys())
data = np.array(list(results.values()))
data_cum = data.cumsum(axis=1)
category_colors = list(dico_couleur.values())

fig, ax = plt.subplots(figsize=(15, 7))
ax.invert_yaxis()
ax.xaxis.set_visible(False)
ax.set_xlim(0, np.sum(data, axis=1).max())

for i, (colname, color) in enumerate(zip(category_names, category_colors)):
    widths = data[:, i]
    starts = data_cum[:, i] - widths
    ax.barh(labels, widths, left=starts, height=0.5,
            label=colname, color=color)
    xcenters = starts + widths / 2


    for y, (x, c) in enumerate(zip(xcenters, widths)):
        if c != 0:
            ax.text(x, y, str(int(c)), ha='center', va='center',
                        color=text_color)
ax.legend(ncol=len(category_names), bbox_to_anchor=(1, 0, 0.5, 1),
          loc='lower left', fontsize='small')

plt.show()  

the result gives the following image, but I haven't found a way of breaking line after each category_names enter image description here

Thank You

1 Answers

This is happening because you've specified the ncol parameter in your call to ax.legend(). This is the number of columns that the legend will display. You set this to the length of the category_names variable, which in your example is 7. As a result, the legend shows seven columns, with no line-breaks between each label.

To fix this, just remove this parameter from your function call. It defaults to 1, so will give you the plot below.

ax.legend(bbox_to_anchor=(1, 0, 0.5, 1), loc='lower left', fontsize='small')

enter image description here

Related