如何正确绘制超大刻度?

2024-03-29 12:06:18 发布

您现在位置:Python中文网/ 问答频道 /正文

a tick的文本标签太长时,我们通常需要旋转它以防止文本重叠。我在这里遇到的问题是,当matplotlib的长度超过图形的大小时,它将只显示其中的一部分。你知道吗

<强>1。有没有办法扩展图表底部的边距,使其适合所有文本?

<强>2。如果横向比较好,有没有办法把文本分成两行或多行,这样它们就不会重叠?

下面是一个切断文本的示例: enter image description here

我试着adjust the size

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

但它只会导致图中每个元素的比例增加。你知道吗

我试着用

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()

Tags: the文本图形matplotlibnppltwidthcan
1条回答
网友
1楼 · 发布于 2024-03-29 12:06:18

你可以用

plt.subplots_adjust(bottom=0.3)

以获得更多的空间。默认值为0.1。你知道吗

请注意,使用默认的图形大小,我无法增加空间以使完整(非常长)的文本可见。如果你定义了一个数字大小,那么它应该是可能的。你知道吗

相关问题 更多 >