标签无法在条形图中完全显示

时间:2019-08-01 14:45:14

标签: python matplotlib bar-chart

我目前拥有的条形图可以在https://ibb.co/mcrdV8R中找到。问题是,由于y轴上的某些标签相对较长,因此它们被向左截断。有没有办法完整显示它们(例如缩小条形图部分等),因为我不能使标签更短。另外,如何使条变得更细?预先感谢!

plt.barh(posBP, -np.log10(p_BP), color='#FF8000')
plt.barh(posMF, -np.log10(p_MF), color='#FF3333')
plt.barh(posCC, -np.log10(p_CC), color='#FFB266')
plt.yticks(np.arange(len(BP)+len(MF)+len(CC)),des_CC[::-1]+des_MF[::-1]+des_BP[::-1])
plt.xlabel('-$\log_{10}(p\_value)$')

1 个答案:

答案 0 :(得分:0)

您可以使用barh参数手动调整width容器的厚度,只需使用matplotlib函数tight_layout()即可自动调整图形区域:

import matplotlib.pyplot as plt
import numpy as np

# random data
x = np.arange(1,13,1)
y = np.random.random(len(x))
# Adjust width of your bars here
width = 0.1

fig, ax = plt.subplots(figsize=(11.69,8.27))
ax.barh(x, y, width, color='r') 
ax.set_xlabel('-$\log_{10}(p\_value)$')
ax.set_title('GO terms, Genes biased towards Nsyl in Root')
ax.set_yticks(x)   # set the position of the y ticks
ax.set_yticklabels(('5-triphosphate 6-kinase thingy which is important to know','other tick','and so on...'))
# matplotlib automatically adjusts the layout
plt.tight_layout()
plt.show()

给出:

enter image description here