0

Is there any way I can set up a max width for the labels on the x axis? I have long labels and they're overlapping so I'd like to split a single label in multiple rows. This is how I'm setting labels right now:

    plt.plot(
        [point['label'] for point in data],
        [point['value'] for point in data],
        color=Colors.success,
        marker='o',
        linewidth=1,
        markersize=5,
    )

enter image description here

Mihai Zamfir
  • 2,167
  • 4
  • 22
  • 37

2 Answers2

3
plt.plot([0,10],[0,2])
plt.xticks(np.arange(11),["\n".join("27 Nov 1982".split())]*11)

If your labels are multi words then you can split them with "\n" so that each word of a label appears on a different line.

enter image description here

When the labels are very long strings (not words) then the best way is to rotate them. If for some reasons you don't want to rotate, the other (ugly) way is to split the long string at every n characters into a different row (where n=4 in below example).

labels = ["verylongname{0}".format(i) for i in range(11)]
plt.plot([0,10],[0,2])
plt.xticks(np.arange(11),["\n".join([word[i:i+4] for i in range(0, len(word), 4)]) for word in labels])

enter image description here

mujjiga
  • 16,186
  • 2
  • 33
  • 51
0

You can set the labels on the x axis:

#Every n-th label
positions = np.arange(0,len(point),n)
labels = [point['label'] for point in data][::n]
plt.xticks(positions,labels)

You can rotate them as well.

Ardweaden
  • 857
  • 9
  • 23