轴的标签相互重叠

2024-09-30 14:33:28 发布

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

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

Tags: posimportnumpytargetmatplotlibasfigplt
1条回答
网友
1楼 · 发布于 2024-09-30 14:33:28

你需要做一些不同的工作。你的重叠基本上是因为标签是用字体写的,在你的绘图比例下,字体太大,无法容纳在条形图的高度内。你可以做的事情:

  1. 在调用plt.barh()之前,将您的调用放在前面plt.figure()这样,图形的显示大小将在绘制之前应用。在你的例子中,你想要一个高而不是宽的身材,试着使用尺寸[5,20]
  2. ylim添加到绘图中,并将其仅设置为您使用的y值,这样就不会在条形图的上方和下方获得空白
  3. 将勾号标签字体改为适当的小字体。您试图显示很多条,因此它可能非常小,例如6点。在

应用所有这些-你的代码看起来像(例如):

... 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几乎总是使用matplotlib在代码中引用Figure对象。只是一个惯例-你的代码没有错。在

相关问题 更多 >