Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centered text in matplotlib tables

I am trying to center the text inside a matplotlib table cell, while the default seems to be right aligned. I looked through the documentation of the Table object, but I could not find anything useful in this.

Is there an easy way to achieve the centering?

like image 481
papafe Avatar asked Sep 17 '14 17:09

papafe


People also ask

How do I align text in Matplotlib?

MatPlotLib with PythonPlace legend using legend() method and initialize a method. Iterate the legend. get_texts() method to set the horizontal alignment. To display the figure, use show() method.

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.


2 Answers

Try editing the sample here

Adding

cellLoc='center'

To

the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')

To get enter image description here

like image 128
willo Avatar answered Oct 03 '22 20:10

willo


According to documentation, there is this metthod in cell object :

set_text_props(self, **kwargs)

kwargs may refers to text methods/attribute, such this one:

horizontalalignment or ha = [ 'center' | 'right' | 'left' ]

So, what about :

cell.set_text_props(ha='center')
like image 27
MonsterBat Doppelgänger Avatar answered Oct 03 '22 20:10

MonsterBat Doppelgänger