Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split title of a figure in matplotlib

Tags:

matplotlib

I use matplotlib to create a figure with 4 sub-plots in it.

I would like to split one of my title of a subplot, such that each line would be in the centered with respect to subplot.

I tried

import matplotlib.pylab as plt  fig = plt.figure(num=0,figsize=(8.27, 11.69), dpi=300) ax  = fig.add_subplot(2, 2, 1) ax.set_title(r'Normalized occupied \\ Neighbors') 

and what i get is that Neighbors is indent to left side.

How could i correct this?

like image 373
Eagle Avatar asked Dec 22 '11 00:12

Eagle


People also ask

How do I split a figure in Matplotlib?

In order to split the figure you should give 3-digit integer as a parameter to subplot() . The integers describe the position of subplots: first digit is the number of rows, the second is the number of columns, and the third is the index of the subplot.

What is plt subplot 121?

The number p increases from 1 up to m x n , and the plots are placed from left to right, and top to bottom. In this case, when you do subplot(1,2,1); or subplot(121); , you would like to have one row and two columns worth of figures. The last number, p=1 means that you wish to place the plot in the left most column.

How do you separate subplots in Python?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.


1 Answers

I get the correct alignment when I format the string this way:

import matplotlib.pylab as plt  fig = plt.figure()#num=0,figsize=(8.27, 11.69), dpi=300) ax  = fig.add_subplot(2, 2, 1) ax.set_title('Normalized occupied \n Neighbors')  plt.show() 

enter image description here

like image 135
Yann Avatar answered Sep 28 '22 04:09

Yann