1

When the text label of a tick is too long, often time we need to rotate it to prevent overlapping of texts. The issue I am having here is that matplotlib will display only a portion of it when it is longer than the size of the graph.

1. Is there a way to extend the margin of the bottom of the graph so that it fits all the text?

2. If horizontal is more preferable, is there a way to put text into two or more lines so they don't overlap?

Here is an example that cutting off the text: enter image description here

I tried to adjust the size with

plt.figure(figsize=(50,50))

but it only results in a proportional increase of every element in the graph.

I tried to adjust the margin with

plt.tight_layout()

but it seems to trade off with the graph content. Finally, thought of adjusting the aspect ratio, but it seems to related to the content of the graph only.

I am currently using matplotlib 2.2.2 and you may reproduce the graph above with the following code (original):

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

# plt.figure(figsize=(500,20))

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
             bottom=menMeans, yerr=womenStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
# plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.xticks(ind, ('It shows up perfectly fine in Jupyter Notebook', 'how can I adjust "some setting" so that I can see all these?', 'G3', 'G4', 'G5'))
plt.tick_params(labelrotation=90)
# plt.savefig('Example.jpeg')

# plt.tight_layout()

plt.show()
Anthony Lei
  • 241
  • 1
  • 9

1 Answers1

0

You can use

plt.subplots_adjust(bottom=0.3)

to get more space under the plot. The default value is 0.1.

Note that with the default figure size, I could not increase the space so that the complete (very long) text is visible. If you define a figure size, it should be possible, though.

cheersmate
  • 2,385
  • 4
  • 19
  • 32