0

I have the following code:

def plot_yz_over_x_elementwise(zarray):
    for i in range(zarray.shape[1]):
        for j in range(zarray.shape[2]):
            plt.subplot2grid((zarray.shape[1],zarray.shape[2]),(i,j))
            plt.plot(zarray[:,i,j])
            frame = plt.gca()
            frame.axes.get_xaxis().set_ticks([])
            frame.axes.get_yaxis().set_ticks([1,0.5,0])

Which takes a 3D np array and plots it in a figure like this:

enter image description here

Which is embedded in an ipython notebook. What I need is some way to format this graph so that the axis labels aren't on top of each other. I guess that means either spreading it out on the notebook...or shrinking it within each cell in python. How do I do one or the other to get this plot looking right?

Thomas K
  • 39,200
  • 7
  • 84
  • 86
Chris
  • 28,822
  • 27
  • 83
  • 158

2 Answers2

2

tight_layout might be what you're looking for

def plot_yz_over_x_elementwise(zarray):
    for i in range(zarray.shape[1]):
        for j in range(zarray.shape[2]):
            plt.subplot2grid((zarray.shape[1],zarray.shape[2]),(i,j))
            plt.plot(zarray[:,i,j])
            frame = plt.gca()
            frame.axes.get_xaxis().set_ticks([])
            frame.axes.get_yaxis().set_ticks([1,0.5,0])
    plt.tight_layout()
gsmafra
  • 2,434
  • 18
  • 26
0

Pretty sure this a duplicate of this question

Bottomline the answer is to use plt.tight_layout

Community
  • 1
  • 1
Jim Parker
  • 1,095
  • 11
  • 16
  • Hey, there is virtue to having alternative search titles. I definitely searched for this, and found nothing. Presumably, if I were an expert, I would have found those links...but if I were an expert, I wouldn't have a question. In other words, thanks for pointing that out...but I'd never have found those search lines. – Chris Feb 27 '16 at 16:01