Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing frame while keeping axes in pyplot subplots

I am creating a figure with 3 subplots, and was wondering if there is any way of removing the frame around them, while keeping the axes in place?

like image 788
branwen85 Avatar asked Feb 25 '14 14:02

branwen85


People also ask

How do I hide a spine in matplotlib?

To turn the spines off - you can access them via the ax. spines dictionary. Using their keys, top , bottom , left , and right , you can select each one, and using the set_visible() function, turn them off.

How do I leave the space between subplots?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.


2 Answers

If you want to remove the axis spines, but not the other information (ticks, labels, etc.), you can do that like so:

fig, ax = plt.subplots(7,1, sharex=True)  t = np.arange(0, 1, 0.01)  for i, a in enumerate(ax):     a.plot(t, np.sin((i + 1) * 2 * np.pi * t))     a.spines["top"].set_visible(False)     a.spines["right"].set_visible(False)     a.spines["bottom"].set_visible(False) 

or, more easily, using seaborn:

fig, ax = plt.subplots(7,1, sharex=True)  t = np.arange(0, 1, 0.01)  for i, a in enumerate(ax):     a.plot(t, np.sin((i + 1) * 2 * np.pi * t))  seaborn.despine(left=True, bottom=True, right=True) 

Both approaches will give you:

enter image description here

like image 150
mwaskom Avatar answered Sep 18 '22 23:09

mwaskom


Try plt.box(on=None) It removed only the bounding box (frame) around plot, which is what I was trying to do.

plt.axis('off') removed tick labels and the bounding box, which wasn't what I was looking to accomplish.

like image 31
Ben Miller Avatar answered Sep 19 '22 23:09

Ben Miller