Python-Matplotlib Moving a chart title on the y axis

I am currently using matplotlib in python to graphically display some data, however I want the graph headers to be on the Y axis, because there is not enough space for both the title of one graph and the x-axis label for Others. I know that I can simply set hspace to a larger number, but I do not want to do this because I plan to have several diagrams stacked on top of each other, and if I adjust the hspace, then the graph will be really short and difficult to read. like this http://oi39.tinypic.com/2a4r5i1.jpg

Here is my code

#EXAMPLE CODE
import numpy as np
import matplotlib.pyplot as plt


fig=plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

x = [1,2,3,4,5,6,7,8]
y = [4,3,8,2,8,0,3,2]
z = [2,3,0,8,2,8,3,4]


ax1 = fig.add_subplot(2,1,1, axisbg='gray')
ax1.plot(x, y, 'c', linewidth=3.3)
ax1.set_title('title', color='c')
ax1.set_xlabel('xlabel')
ax1.set_ylabel('ylabel')

ax2 = fig.add_subplot(2,1,2, axisbg='gray')
ax2.plot(x, z, 'c', linewidth=3.3)
ax2.set_xlabel('xlabel')
ax2.set_ylabel('ylabel')



plt.show()

thanks in advance

+4
source share
2 answers

Try the following:

ax1.set_title('title1', color='c', rotation='vertical',x=-0.1,y=0.5)
ax2.set_title('title2', color='c', rotation='vertical',x=-0.1,y=0.5)
+6

matplotlib get_position set_position. , . (: , .. (0,0) - (1,1) - )

fig, axes = plt.subplots(nrows=2)
ax0label = axes[0].set_ylabel('Axes 0')
ax1label = axes[1].set_ylabel('Axes 1')

title = axes[0].set_title('Title')

offset = np.array([-0.15, 0.0])
title.set_position(ax0label.get_position() + offset)
title.set_rotation(90)

fig.tight_layout()

enter image description here

+3

All Articles