0
votes

I have the following code for which only ax3 displays the plotting area and data. Subplots ax1 and ax2 display a plot area but no data. Ultimately, I want three plots (3, 1 configuration) with sharex=True. I'm using Python 2.7.2 and matplotlib 1.2.1

import numpy as np
import matplotlib.pyplot as plt

f, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, sharey=False)
f.subplots_adjust(wspace=0)

N = 4
ind = np.arange(N)

width = .4
group_names = ('A', 'B', 'C', 'D')

q2 = [84, 21, 10, 4]
q4 = [69, 34, 22, 0]
q6 = [66, 28, 17, 2]

colors = ['c', 'purple', 'g', 'red']

ax1 = plt.barh(ind, q2, width, color=colors)
plt.yticks(ind+width/N, group_names)
plt.ylim([min(ind)-0.25, max(ind)+0.65])
plt.xticks(np.arange(0, 110, 10.))
plt.xlim(0, 100)

ax2 = plt.barh(ind, q4, width, color=colors)
plt.yticks(ind+width/N, group_names)
plt.ylim([min(ind)-0.25, max(ind)+0.65])
plt.xticks(np.arange(0, 110, 10.))
plt.xlim(0, 100)

ax3 = plt.barh(ind, q6, width, color=colors)
plt.yticks(ind+width/N, group_names)
plt.ylim([min(ind)-0.25, max(ind)+0.65])
plt.xticks(np.arange(0, 110, 10.))
plt.xlim(0, 100)

plt.show()
2
Why do you use plt.barh here? Instead use ax1.barh, etc to plot directly to your axis.cel
by the way, matplotlib v1.2.1 is 2 and a half years old. 1.4.3 is the current stable release. You might consider updating your versiontmdavison

2 Answers

1
votes

You've done something a bit strange. You define ax1, ax2, and ax3 at the top of your script with the plt.subplots command, but then you don't use them later. I've reworked you script to make sure that all your commands are working on the correct axes.

Instead of plt.barh(), you can use ax1.barh(). Then, you can set the ticks and axes limits using ax1.set_yticks(), ax1.set_ylim(), ax1.set_xticks() and ax1.set_xlim()

import numpy as np
import matplotlib.pyplot as plt

f, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, sharey=False)
f.subplots_adjust(wspace=0)

N = 4
ind = np.arange(N)

width = .4
group_names = ('A', 'B', 'C', 'D')

q2 = [84, 21, 10, 4]
q4 = [69, 34, 22, 0]
q6 = [66, 28, 17, 2]

colors = ['c', 'purple', 'g', 'red']

ax1.barh(ind, q2, width, color=colors)
ax1.set_yticks(ind+width/N, group_names)
ax1.set_ylim([min(ind)-0.25, max(ind)+0.65])
ax1.set_xticks(np.arange(0, 110, 10.))
ax1.set_xlim(0, 100)

ax2.barh(ind, q4, width, color=colors)
ax2.set_yticks(ind+width/N, group_names)
ax2.set_ylim([min(ind)-0.25, max(ind)+0.65])
ax2.set_xticks(np.arange(0, 110, 10.))
ax2.set_xlim(0, 100)

ax3.barh(ind, q6, width, color=colors)
ax3.set_yticks(ind+width/N, group_names)
ax3.set_ylim([min(ind)-0.25, max(ind)+0.65])
ax3.set_xticks(np.arange(0, 110, 10.))
ax3.set_xlim(0, 100)

plt.show()

enter image description here

0
votes

Instead of using

    plt.barh(ind, q2, width, color=colors)

change them as

    ax1 = ax1.barh(ind, q2, width, color=colors)
    ax2 = ax2.barh(ind, q4, width, color=colors)
    ax3 = ax3.barh(ind, q6, width, color=colors)

Hope this solves your problem.