【问题标题】:Wrap text based on axis limits instead of chart limits, twinx matplotlib根据轴限制而不是图表限制换行文本,twinx matplotlib
【发布时间】:2021-10-09 19:49:48
【问题描述】:

我一直在尝试将文本包装在使用 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)

【问题讨论】:

    标签: python matplotlib word-wrap twinx


    【解决方案1】:

    您可以尝试使用 ax.text 的换行功能:

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.set_xlim(0,4)
    ax.set_ylim(0,4)
    string = 'this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long string'
    txt = ax.text(1, 1, string, ha='left', rotation=0, wrap=True)
    txt._get_wrap_line_width = lambda : 200
    plt.show()
    

    您也可以关注demo

    这是我上面的代码生成的图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多