user3909274
user3909274

Reputation: 31

horizontal bar plot: adjusting y axes label size

using pandas I tried to make a horizontal bar plot, but the y-axes label is cut off. for the plot I used this data..

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

For y-axes label, I used "Term" column.

my code is

    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')

an example of horizontal bar plot looks like this : plot

how can i fix it? instead of saving the image I also tried to make and write the plot in Excel file using pandas and XlsxWriter (pandas and XlsxWriter), but it seems XlsxWriter doesn't have the function to draw the horizontal bar plot.

Upvotes: 3

Views: 1778

Answers (1)

jrjc
jrjc

Reputation: 21873

You can do this :

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

Here you gain room with :

  • fontsize smaller
  • larger figure
  • splitting your text on different lines :

.

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

])

Hope this helps, and don't hesitate if you have questions.

Upvotes: 3

Related Questions