Python 水平条形图:调整y轴标签大小

Python 水平条形图:调整y轴标签大小,python,matplotlib,pandas,Python,Matplotlib,Pandas,我尝试使用熊猫绘制水平条形图,但y轴标签被切断。 对于绘图,我使用了这些数据 Term value GO:0043232~intracellular non-membrane-bounded organelle 69.40887024 GO:0043228~non-membrane-bounded organelle 68.41919153

我尝试使用熊猫绘制水平条形图,但y轴标签被切断。 对于绘图,我使用了这些数据

Term                                                           value
GO:0043232~intracellular non-membrane-bounded organelle    69.40887024
GO:0043228~non-membrane-bounded organelle                  68.41919153
GO:0051384~response to glucocorticoid stimulus              58.50901338
hsa04310:Wnt signaling pathway                             24.56895837
GO:0016055~Wnt receptor signaling pathway                   18.00929324
GO:0042127~regulation of cell proliferation             5.215295969
对于y轴标签,我使用了“术语”列

我的代码是

    a=list(final_table['value'])
    b=list(final_table['Term'])
    data=pd.DataFrame(a,index=b,columns=['value'])
    data[:31].plot(kind='barh',color='k',alpha=0.7)
    matplotlib.pyplot.savefig('test.png')
水平条形图的示例如下所示:

我怎样才能修好它?除了保存图像,我还尝试使用pandas和XlsxWriter()在Excel文件中绘制和写入绘图,但XlsxWriter似乎没有绘制水平条形图的功能。

您可以这样做:

In [1]: data
Out[1]: 
                                                             value
Term                                                              
GO:0043232~intracellular non-membrane-bounded organelle  69.408870
GO:0043228~non-membrane-bounded organelle                68.419192
GO:0051384~response to glucocorticoid stimulus           58.509013
hsa04310:Wnt signaling pathway                           24.568958
GO:0016055~Wnt receptor signaling pathway                18.009293
GO:0042127~regulation of cell proliferation               5.215296


In [2]: data.plot(kind="barh", fontsize=9, figsize=(15,8)) 
In [3]: plt.yticks(range(6), 
                   ["\n".join(
                              [" ".join(i.split("~")[:-1]),
                              "\n".join(i.split("~")[-1].split(" "))])
                   for i in a.index])

在这里,您可以通过以下方式获得空间:

  • 字体大小较小
  • 较大数字
  • 在不同的行上拆分文本:

希望这对您有所帮助,如果您有任何疑问,请不要犹豫。

您可以在XlsxWriter中绘制一个。
"\n".join([

" ".join(i.split("~")[:-1]), # everything before the "~" () actually [0] but since the 4th line has no "~", I put all but last to avoid redundancy.

"\n".join(i.split("~")[-1].split(" ")) # here line break every word.

])