2

My plan is to have 4 GridSpec(4,1) gridspecs to create a 4x4 grid of subplots. I would like to add a horizontal line across the x-axes of each row of 4 subplots. I looked at matplotlib.lines.Line2D but couldn't really figure it out. Any suggestions? I'm trying to visually simplify the picture so it does not look like 16 separate graphs.

In the picture below I only have the first 2 gridspecs up, but I hope it offers a better idea of what I am hoping to achieve.

Thanks! Cheers

Code (the graph part):

#---the graph---
fig = plt.figure(facecolor='white')

gs1 = GridSpec(4,1)
gs1.update(left = 0.15, right = .3375 , wspace=0.02)

ax1 = plt.subplot(gs1[3,0])
ax2 = plt.subplot(gs1[2,0])
ax3 = plt.subplot(gs1[1,0])
ax4 = plt.subplot(gs1[0,0])



gs2 = GridSpec(4,1)
gs2.update(left = 0.3875, right = .575, wspace=.25)

ax1 = plt.subplot(gs2[3,0])
ax2 = plt.subplot(gs2[2,0])
ax3 = plt.subplot(gs2[1,0])
ax4 = plt.subplot(gs2[0,0])


show()

enter image description here

9
  • Can you post the existing code? Commented May 1, 2014 at 17:44
  • @MrE for sure, i hope it's not too disorganized Commented May 1, 2014 at 17:47
  • 1
    Does this one help? stackoverflow.com/questions/22840621/…, See the commented line: '#the above three lines to draw the line out of the box.' Commented May 1, 2014 at 18:03
  • @CTZhu I feel like the answer I am looking for is in there, but I can't seem to figure it out. based on the comments in the code I have been focusing on the ll = plt.plot(xb,yb, '-b') ll[0].set_clip_on(False) plt.axis([0,100,0,1.2]) section... however I am still pretty lost. Is that the right part to look at? Commented May 1, 2014 at 18:15
  • There is way too much code here. Please trim it down to a minimal example that demonstrates your problem. Commented May 1, 2014 at 19:32

1 Answer 1

2

Basically the idea is to draw a line and allow the line to extend beyond the current view of axis, in this following example, I plot that line in red in order to see it better.

Also your 8 plots can be plotted in a nested loop, which will organize the code better and make this 'common line across subplot' easier to implement:

X=[1,3,4,5]
Y=[3,4,1,3]
L=['A', 'B', 'C', 'D']
f=plt.figure(figsize=(10,16), dpi=100)
gs1 = gridspec.GridSpec(4,1)
gs1.update(left = 0.15, right = .3375 , wspace=0.02)
gs2 = gridspec.GridSpec(4,1)
gs2.update(left = 0.3875, right = .575, wspace=.25)
sp1 = [plt.subplot(gs1[i,0]) for i in range(4)]
sp2 = [plt.subplot(gs2[i,0]) for i in range(4)]
for sp in [sp1, sp2]:
    for ax in sp:
        ax.bar(range(len(L)), X, 0.35, color='r')
        ax.bar(np.arange(len(L))+0.35, Y, 0.35)
        ax.spines['right'].set_visible(False)
        ax.yaxis.set_ticks_position('left')
        ax.spines['top'].set_visible(False)
        ax.xaxis.set_ticks_position('bottom')
        if sp==sp1:
            ax.axis(list(ax.get_xlim())+list(ax.get_ylim())) #set the axis view limit
            ll=ax.plot((0,10), (0,0), '-r') #Let's plot it in red to show it better
            ll[0].set_clip_on(False) #Allow the line to extend beyond the axis view
plt.savefig('temp.png')            

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That is exactly what I was looking for. Unfortunately I don't think i'll be able to use the nested loop though because I want the xticklabels to only show for the bottom graphs.
If I were you, I will make the x ticks invisible for each subplot in the nested loop. Then make the x ticks separately for sp1[-1] and sp2[-1].
how would I go about making the ticks separately for sp1[-1]?

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.