当文本标签为a tick时太长了,我们经常需要旋转它以防止文本重叠。我在这里遇到的问题是,当它比图形的大小长时,matplotlib 将仅显示它的一部分。
<强>1。有没有办法扩展图表底部的边距,使其适合所有文本?
<强>2。如果水平更可取,有没有办法将文本放入两行或多行,这样它们就不会重叠?
我尝试adjust the size与
plt.figure(figsize=(50,50))
但它只会导致图中每个元素按比例增加。
我尝试调整 margin与
plt.tight_layout()
但它似乎与图形内容进行了权衡。最后想到adjusting the aspect ratio ,但似乎只与图的内容有关。
我目前正在使用 matplotlib 2.2.2 和 您可以使用以下代码重现上图( 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()
最佳答案
你可以使用
plt.subplots_adjust(bottom=0.3)
在绘图下获得更多空间。默认值为 0.1。
请注意,使用默认的图形大小,我无法增加空间以使完整(很长)的文本可见。不过,如果您定义了图形尺寸,那么应该是可以的。
关于python - 如何正确绘制超大刻度线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531001/