我一直在尝试将文本包装在使用 matplotlib 制作的图表中。我现在的问题是我使用 twinx 制作了图表,但我想要包装的文本是关于整个图表而不是我希望它只在轴上的包装。我不希望文本与条重叠。有没有办法来解决这个问题?
for i in stocks:
#WRITE PIPELINE
ppp = PipelineData['Ticker']
ccc = PipelineData['Catalyst']
cc = ccc.loc[ppp==i]
label = cc+str(" ")
#label = [ '\n'.join(wrap(l, 50)) for l in label ]
dd = PipelineData['Drug']
drugs = dd.loc[ppp==i]
drugs = drugs+str(" ")
#drugs = [ '\n'.join(wrap(drugs, 35)) for d in drugs ]
pp = PipelineData['Phase']
p = pp.loc[ppp==i]
disdis = PipelineData['Disease']
dis = disdis.loc[ppp==i]
figure = plt.figure(figsize=(11,6.3))
ax1 = figure.add_subplot()
ax2 = ax1.twinx()
width = 0.7 # the width of the bars
ind = np.arange(len(p)) # the x locations for the groups
ax1.barh(ind, p, width, color="#67a362", align='edge')
ax1.set_yticks(ind+width/2)
ax1.set_yticklabels(drugs, minor=False, fontname= 'Times New Roman', wrap=True)
ax1.set_ylabel('Drug', fontname= 'Times New Roman', wrap=True)
ax1.set_xmargin(1)
ax1.autoscale_view()
ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks(np.arange(len(label)) + width/2)
ax2.set_yticklabels(label,horizontalalignment = "right", fontname='Times New Roman', wrap=True) #CAN CHANGE HORIZONTALALLIGNMENT HERE
ax1.spines['right'].set_color('None')
#ax2.autoscale_view(scalex=False)
#ax2.spines['right'].set_color('None')
plt.xticks(np.arange(5),('Pre-clinical','Phase I','Phase II','Phase III', 'Approved'), fontname='Times New Roman', wrap=True)
plt.rcParams['ytick.major.pad']=-12
plt.rcParams['axes.grid'] = False
plt.rcParams['font.family']='serif'
plt.rcParams['font.size']=12.5
ax1.grid(False)
ax2.grid(False)
ax1.set_facecolor('w')
ax2.set_facecolor('w')
for bar, disease in zip(ax1.patches, dis):
ax1.text(0.1, bar.get_y()+bar.get_height()/2, disease, color = 'black', ha = 'left', va = 'center')
figure.savefig(str(i)+'newpipe.png', bbox_inches=None, wrap=True)
ws.insert_image(row_pipe,col_pipe,str(i)+'newpipe.png',{'x_scale':.56, 'y_scale':.56})
row_pipe += page_len
plt.close(figure)