Male_population = įemale_population = įig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)Īx1.set_title('Total Population of India')Īx1.plot(ages, total_population, label='Total Population')Īx2.plot(ages, male_population, label='Male')Īx2.plot(ages, female_population, label='Female') Let us create two Matplotlib subplots stacked vertically, we will be using population data for men and women for one Matplotlib Subplot and total population for other subplot. # matplotlib_subplot_python.pyĪx.set_title('Total Population of India')Ĭreate Matplotlib Subplot with one figure and two axes Unlike plt.plot, we have to ax.set_title(), ax.set_xlabel() and ax.set_ylabel(). We will create a Matplotlib Subplot for the population data as done in Chapter 1, but instead of using plt.plot() directly, we will be creating a subplot and then plotting the data. The following examples will make it more clear.Ĭreate Matplotlib Subplot with one figure and one axes Plt.subplot() takes nrows and ncols as argument, which are 1 as default, hence just calling plt.subplots() has returned one figure and one axes. Let us create an new file ‘matplotlib_subplot_python.py’ # matplotlib_subplot_python.py (ii) ax : ax can be either a single Axes object or an array of Axes objects if more than one subplot was created. It is the window which holds the graph/grid/axes. (i) fig : A Figure is the window which is returned on calling plt.show(). But using Matplotlib subplots, we can create mutliple figures and grids. Matplotlib.pyplot() or plt was automatically creating the plot which had one figure and one grid. But now, we will be using () (ptl.subplots()) to create Matplotlib Subplots. Till now, we have been using matplotlib.pyplot() to create the plots. In this Matplotlib Subplot tutorial, we will be learning to create Matplotlib Subplots. G4.Matplotlib Subplot in Python | Chapter 10 G2.set_title("Systolic vs Diastolic blood pressure") df.plot(ax = g4, color='gray') G3.set_title("Distribution of systolic blood pressure") df.plot('BPXSY1', 'BPXDI1', kind='scatter', ax = g2, alpha=0.3) G1.set_title("Bar plot of Systolic blood pressure for different marital status") df.hist(ax = g3, orientation = 'horizontal', color='gray') G4 = plt.subplot(grid) df.groupby('DMDMARTL').mean().plot(kind = 'bar', ax = g1) Grid = plt.GridSpec(4, 4, wspace =0.3, hspace = 0.8) g1 = plt.subplot(grid) Now as you know how to index the grid and make custom-shaped plots, let’s make another one and put some real plots in them. ‘grid’ means row index 2 and column-index 2 to end. The column index starts at 2 and goes till the end. So the column index becomes 0:2 which can be written as :2. The column-index starts at 0 and takes 2 plots. The row index starts at 1 and goes till the end. Using ‘grid’ we are making that big square-shaped one. 0 means row-index is 0 and 3 means column-index is 3. But when it starts with 0, it can be written as :3. Because it is the first row, row-index is 0, and column index is 0 to 3 as we are taking the first three columns. ‘grid’ here is taking the first three plots of the first row and making a bigger plot. Using this code we are indexing the grid and making a custom shape. Next, we indexed through the grids to make custom sizes of plots. fig, ax = plt.subplots(2, 3, figsize = (15, 10))įig.tight_layout(pad = 2) ax.scatter(df, df)ĭf.groupby('DMDEDUC2').mean().plot(ax = ax, kind='pie', colors = ) So, here is how to access the ‘ax’ elements and set plots in them. We will access each ‘ax’ element by indexing simply like a two-dimensional array. I will make a 2×3 array of plots again and set plots in the ‘ax’ elements. Here I am importing a dataset using pandas: df = pd.read_csv('nhanes_2015_2016.csv') Please feel free to download the dataset and follow along.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |