轴的标签彼此重叠

时间:2015-03-27 12:30:30

标签: python matplotlib overlap axes names

我正在使用matplotlib生成条形图。这一切都运作良好,但我无法弄清楚如何防止轴标签相互重叠。这是一个例子:

#!/usr/bin/env python
import matplotlib.pyplot as plt
import matplotlib.ticker as tic
import csv
from numpy import *
import matplotlib.pyplot as plt
from numpy.random import normal
import numpy as np



target_LandQ=['CHEMBL1293235', 'CHEMBL220', 'CHEMBL289', 'CHEMBL2903', 'CHEMBL311', 'CHEMBL3199', 'CHEMBL3356', 'CHEMBL3397', 'CHEMBL340', 'CHEMBL3622', 'CHEMBL4078', 'CHEMBL5501', 'CHEMBL1075216', 'CHEMBL1075280', 'CHEMBL1075282', 'CHEMBL1798', 'CHEMBL1821', 'CHEMBL1824', 'CHEMBL1827', 'CHEMBL1832', 'CHEMBL1833', 'CHEMBL1835', 'CHEMBL1841', 'CHEMBL1867', 'CHEMBL1868', 'CHEMBL1889', 'CHEMBL1901', 'CHEMBL1909043', 'CHEMBL1909044', 'CHEMBL1914', 'CHEMBL1916', 'CHEMBL1941', 'CHEMBL1942', 'CHEMBL1951', 'CHEMBL1977', 'CHEMBL203', 'CHEMBL2034', 'CHEMBL2035', 'CHEMBL205', 'CHEMBL2056', 'CHEMBL206', 'CHEMBL210', 'CHEMBL211', 'CHEMBL213', 'CHEMBL216', 'CHEMBL217', 'CHEMBL218', 'CHEMBL219', 'CHEMBL221', 'CHEMBL222', 'CHEMBL223', 'CHEMBL224', 'CHEMBL225', 'CHEMBL226', 'CHEMBL228', 'CHEMBL2292', 'CHEMBL230', 'CHEMBL231', 'CHEMBL2327', 'CHEMBL233', 'CHEMBL234', 'CHEMBL236', 'CHEMBL237', 'CHEMBL238', 'CHEMBL240', 'CHEMBL2414', 'CHEMBL242', 'CHEMBL2434', 'CHEMBL245', 'CHEMBL246', 'CHEMBL248', 'CHEMBL2487', 'CHEMBL249', 'CHEMBL250', 'CHEMBL251', 'CHEMBL252', 'CHEMBL256', 'CHEMBL258', 'CHEMBL259', 'CHEMBL260', 'CHEMBL2622', 'CHEMBL273', 'CHEMBL274', 'CHEMBL287', 'CHEMBL299', 'CHEMBL3048', 'CHEMBL3072', 'CHEMBL315', 'CHEMBL3157', 'CHEMBL3198', 'CHEMBL321', 'CHEMBL3243', 'CHEMBL326', 'CHEMBL332', 'CHEMBL3371', 'CHEMBL3385', 'CHEMBL3403', 'CHEMBL3459', 'CHEMBL3464', 'CHEMBL3577', 'CHEMBL4015', 'CHEMBL4018', 'CHEMBL402', 'CHEMBL4029', 'CHEMBL4040', 'CHEMBL4071', 'CHEMBL4074', 'CHEMBL4358', 'CHEMBL4376', 'CHEMBL4445', 'CHEMBL4607', 'CHEMBL4608', 'CHEMBL4644', 'CHEMBL4768', 'CHEMBL4777', 'CHEMBL4780', 'CHEMBL4801', 'CHEMBL4822', 'CHEMBL5017', 'CHEMBL5077', 'CHEMBL5144', 'CHEMBL5281', 'CHEMBL5282', 'CHEMBL5486', 'CHEMBL5763']
hist_q=[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]


y_pos = np.arange(len(hist_q))
plt.barh(y_pos, hist_q, align='center', alpha=0.4)
plt.yticks(y_pos, target_LandQ)
plt.figure(1, [20, 20])
plt.xlabel('Target Presence')
plt.title('Query Molecule')
fig="s_Q"
plt.savefig(fig,bbox_inches='tight')
plt.show()
fig = plt.figure()
fig.clf()

ax = fig.add_subplot(111)
ax.yaxis.grid(True, linestyle='-', which='major', color='grey', alpha=0.5)
ind = range(len(hist_q))
rects = ax.bar(ind, hist_q, width=0.4444, align='center', color='thistle')
ax.set_xticks(ind)
ax.set_xticklabels(target_LandQ)

fig = plt.figure(1, [20, 8])
ax.set_xlim(-1,100)
plt.setp(ax.get_xticklabels(), fontsize=7, rotation='vertical')
fig.autofmt_xdate()
plt.savefig('test2.png',  dpi = 300,  pad_inches = 0)
plt.show()

1 个答案:

答案 0 :(得分:0)

你需要在这里做几个不同的位。您的重叠基本上是由于标签是用字体书写的,这种字体在图表的比例下太大而不适合条形的高度。你可以做的事情:

  1. 在致电plt.figure()之前,将您的电话直接拨打plt.barh() - 这样您就可以在投标之前应用图表的显示尺寸。在您的情况下,您想要一个高而不宽的数字,尝试使用大小[5,20]
  2. 在您的地图中添加ylim,并将其设置为您使用的y值,这样就不会在条形图上方和下方留下空白
  3. 将刻度标签字体更改为适当的小字体。你试图显示很多条形,所以它可能很小 - 例如6点。
  4. 应用所有这些 - 您的代码看起来像(例如):

    ... Your existing code up to...
    
    y_pos = np.arange(len(hist_q))
    plt.figure(1, [5, 20]) # this line moved and figure size changed to suit data
    plt.ylim(0,max(y_pos)+1) # this gets rid of whitespace
    plt.tick_params(axis='both', which='major', labelsize=6) # makes axis labels smaller
    
    ... Carry on existing code from plt.barh(y_pos, hist_q, align='center', alpha=0.4) ...
    

    您可以对第二个图上的x轴执行相同的操作。这些设置通常需要花费一些时间才能使情节看起来完全符合您的要求。

    P.S。

    我不会使用fig为变量名称包含要调用savefig的数字的字符串名称。它会正常工作,但会让您的代码难以阅读,因为fig几乎总是使用Figure在代码中引用matplotlib对象。只是一个惯例 - 你的代码没有错。