python - 水平条形图 : adjusting y axes label size

标签 python matplotlib pandas

我尝试使用 pandas 绘制水平条形图,但 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')

水平条形图的示例如下所示:plot

我该如何解决?我没有保存图像,而是尝试使用 pandas 和 XlsxWriter ( pandas and 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])

barh

在这里你获得空间:

  • 字体变小
  • 更大的身材
  • 将您的文本分成不同的行:

.

"\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.

])

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

关于python - 水平条形图 : adjusting y axes label size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134204/

相关文章:

python - Pandas 用正则表达式读取 csv

python - 如何在 Python 中以 block 的形式读取包含未排序表格数据的大文件?

python - json.decoder.JSONDecodeError : Expecting value: line 1 column 1 (char 0)

python - 在 Mountain Lion 上成功安装后 Matplotlib 无法正常工作

python - 是否有用于扩展日期的 python 函数(类似于 power query 中的 List.Dates)?

python - 在 matplotlib 中叠加 imshow 图

python - 如何在matplotlib中的子图下方添加图例?

python - 拼接并组合两列以形成新的数据框(Pandas)

python - 对象列表的自定义排序

Python/Tkinter - 单击时识别对象