Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the extra plot in the matplotlib subplot

I want to plot 5 data frames in a 2 by 3 setting (i.e. 2 rows and 3 columns). This is my code: However there is an extra empty plot in the 6th position (second row and third column) which I want to get rid of it. I am wondering how I could remove it so that I have three plots in the first row and two plots in the second row.

import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, ncols=3)  fig.set_figheight(8) fig.set_figwidth(15)    df[2].plot(kind='bar',ax=axes[0,0]); axes[0,0].set_title('2')  df[4].plot(kind='bar',ax=axes[0,1]); axes[0,1].set_title('4')  df[6].plot(kind='bar',ax=axes[0,2]); axes[0,2].set_title('6')  df[8].plot(kind='bar',ax=axes[1,0]); axes[1,0].set_title('8')  df[10].plot(kind='bar',ax=axes[1,1]); axes[1,1].set_title('10')  plt.setp(axes, xticks=np.arange(len(observations)), xticklabels=map(str,observations),         yticks=[0,1])  fig.tight_layout() 

Plots

like image 704
HimanAB Avatar asked Jul 07 '17 22:07

HimanAB


People also ask

How do you delete a plot in Matplotlib?

Set the figure size and adjust the padding between and around the subplots. Plot line1 and line2 using plot() method. Pop the second line and remove it.

How do I get rid of the gap 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.

How do I stop subplots from overlapping in Matplotlib?

Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.


1 Answers

Try this:

fig.delaxes(axes[1][2]) 

A much more flexible way to create subplots is the fig.add_axes() method. The parameters is a list of rect coordinates: fig.add_axes([x, y, xsize, ysize]). The values are relative to the canvas size, so an xsize of 0.5 means the subplot has half the width of the window.

like image 53
Johannes Avatar answered Oct 08 '22 19:10

Johannes