9

I have the following code which produces the plot shown.

mport matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec
import numpy as np

One = range(1,10)
Two = range(5, 14) 
l = len(One)
fig = plt.figure(figsize=(10,6))
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0])
ax0.bar(range(l), Two)
plt.ylabel("Number of occurrence")

ax1 = plt.subplot(gs[1], sharey=ax0)
ax1.bar(range(l), Two)

ax2 =  plt.subplot(gs[2])
ax2.bar(range(l), One)

plt.show()

enter image description here

I want the ylabel (" Number of occurrence") to be shared between first and second plot, that is, it should occur in the center left of first and second plot. How do I do that?

DurgaDatta
  • 3,952
  • 4
  • 27
  • 34

4 Answers4

4

The best way I can think of is to add the text to the figure itself and position it at the center (figure coordinates of 0.5) like so

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

One = range(1,10)
Two = range(5, 14)
l = len(One)
fig = plt.figure(figsize=(10,6))
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3])

ax0 = plt.subplot(gs[0])
ax0.bar(range(l), Two)

ax1 = plt.subplot(gs[1], sharey=ax0)
ax1.bar(range(l), Two)

ax2 =  plt.subplot(gs[2])
ax2.bar(range(l), One)

fig.text(0.075, 0.5, "Number of occurrence", rotation="vertical", va="center")

plt.show()
cimarron
  • 421
  • 2
  • 7
1

You can also adjust the position manually, using the y parameter of ylabel:

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec
import numpy as np

One = range(1,10)
Two = range(5, 14) 
l = len(One)
fig = plt.figure(figsize=(10,6))
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0])
ax0.bar(range(l), Two)
plt.ylabel("Number of occurrence", y=-0.8)          ##  ← ← ← HERE

ax1 = plt.subplot(gs[1], sharey=ax0)
ax1.bar(range(l), Two)

ax2 =  plt.subplot(gs[2])
ax2.bar(range(l), One)

plt.show()

enter image description here

Luis
  • 3,327
  • 6
  • 35
  • 62
0

not a very sophisticated solution, but would this work? Replace

plt.ylabel("Number of occurrence")

with

plt.ylabel("Number of occurrence", verticalalignment = 'top')

[edit]

for my version of 64 bit python (2.7.3) I needed to make a small change

plt.ylabel("Number of occurrence", horizontalalignment = 'right')

here's what it looks like to me, is this not what you want?

enter image description here

Brad
  • 1,367
  • 1
  • 8
  • 17
  • This does not put the label in the center of first two plots. – DurgaDatta Dec 18 '13 at 15:16
  • @DurgaDatta I've added what the image looks like on my computer, is that the requirement? – Brad Dec 18 '13 at 16:22
  • This would do my job. However, I do not get this sort of image in my machine. Where do you put that line? Should it matter? – DurgaDatta Dec 18 '13 at 16:26
  • just replace you're ylabel with the one I have above – Brad Dec 18 '13 at 16:28
  • I tried it on python 2.7.3 32 bit and it worked, when I tried it on 2.7.3 64 bit it didn't. If you are using 64 bit then try `plt.ylabel("Number of occurrence", horizontalalignment = 'right')` instead – Brad Dec 18 '13 at 16:53
0

I think this question is more of creating multiple-axis if said in the right terms. I would like to point you to the question I asked and the answer I received which gives you the solution to create multiple-axis for the plot.

Link to the similar problem solution in Matplotlib: Using Multiple Axis

Link to the other related question is:multiple axis in matplotlib with different scales

Community
  • 1
  • 1
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88