3
diff  = [[10,20,30],[40,50,60],[70,80,90]]
comp = ["foo","bar","baz"]
fig,ax = plt.subplots()
for foo in range(0, len(diff)):
        x = [diff[foo]]
        name = comp
        color = ['0.1', '0.2', '0.3']
        label = ['1000000','1200000', '1400000']
        y = zip(*x)
        pos = np.arange(len(x))
        width = 1. / (1 + len(x))

        fig = plt.subplot(3,1,foo)
        for idx, (serie, color,label) in enumerate(zip(y, color,label)):
                ax.bar(pos + idx * width, serie, width, color=color,label=label)
        fig = plt.gcf()
        fig.set_size_inches(28.5,10.5)

        ax.set_xticks(pos + 1.5*width)
        plt.ylabel(name[foo])
        ax.set_xticklabels(comp)
        ax.legend()
        plt.gray()
plt.savefig("file" + '.jpg', bbox_inches='tight', pad_inches=0.5,dpi=100)
plt.clf()

I want to subplot foo bar and baz. But when I try to do that using the above code. The data is not being displayed on the graph. Any idea why?

2
  • I found a very nasty shortcut, which I don't like. Using Montage to join the images. :) Something like this montage -geometry 500 image1 image2 [...] output Commented Nov 20, 2013 at 14:52
  • You've called subplots twice: once before the loop with no arguments, which creates a figure with just one axis, but then in the loop you call it again with (3,1, ...) You want to move that second call to the top and then plot once in each of the three axes. Commented Nov 20, 2013 at 15:00

1 Answer 1

2

You are replacing the first fig when you call subplot inside the loop, here is a fixed version. See that ax returned by subplots is a np.ndarray, so you have to give an index ax[foo] to obtain the AxesSubplot object.

diff  = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
comp = ["foo", "bar", "baz"]
fig, ax = plt.subplots(3, 1)
for foo in range(0, len(diff)):
        x = [diff[foo]]
        name = comp
        color = ['0.1', '0.2', '0.3']
        label = ['1000000', '1200000', '1400000']
        y = zip(*x)
        pos = np.arange(len(x))
        width = 1. / (1 + len(x))
        for idx, (serie, color,label) in enumerate(zip(y, color,label)):
                ax[foo].bar(pos + idx * width, serie, width, color=color,label=label)
        fig.set_size_inches(28.5, 10.5)
        ax[foo].set_xticks(pos + 1.5*width)
        plt.ylabel(name[foo])
        ax[foo].set_xticklabels(comp)
        ax[foo].legend()
        plt.gray()
fig.savefig("file" + '.jpg', bbox_inches='tight', pad_inches=0.5, dpi=100)
plt.clf()
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you call gcf() and resize the figure every time through the loop? You should use fig.savefig(...) instead of plt.savefig
@tcaswell thank you! I've just copied the code from the OP and fixed the problem but did not pay enough attention...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.