python - 如何正确绘制超大刻度线?

标签 python matplotlib plot graph

当文本标签为a tick时太长了,我们经常需要旋转它以防止文本重叠。我在这里遇到的问题是,当它比图形的大小长时,matplotlib 将仅显示它的一部分。

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

<强>2。如果水平更可取,有没有办法将文本放入两行或多行,这样它们就不会重叠?

下面是一个截断文本的例子: enter image description here

我尝试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/

相关文章:

python 导入模块 vs 将脚本作为 subprocess.popen 运行

python - Github-API : How do I plot the number of commits to a repo?

python - 为什么在 Windows 7 (x64) 上为 Python v3.4 安装 matplotlib 失败?

python - matplotlib 如何指定时间定位器的开始滴答时间戳?

python - Matplotlib 条形图去除内部线条

python - Matplotlib 3d 线框图未按预期绘制

python - 根据数据帧中值的重复创建计数器

python - 使用在该函数外部的函数中创建的列表,python

python - 在 WXPYTHON 面板中定位 matplotlib 图形时遇到问题

matlab - 具有 3 个向量的等高线图