【问题标题】:How to plot oversize ticks properly?如何正确绘制超大刻度?
【发布时间】:2019-03-03 00:21:53
【问题描述】:

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

【问题讨论】:

标签: python matplotlib plot graph


【解决方案1】:

你可以使用

plt.subplots_adjust(bottom=0.3)

在情节下获得更多空间。默认值为 0.1。

请注意,使用默认图形大小,我无法增加空间以显示完整(很长)的文本。不过,如果你定义一个图形大小,它应该是可能的。

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多