
This parameter accepts a tuple with width and height.
#Subplot matplotlib python code#
The following code subplots() method takes the figsize parameter.
#Subplot matplotlib python how to#
In the next subsection, we’ll look at how to create multiple plots in one figure.

We have not defined how many rows and columns should be created in a figure because this is the single plot example for basic understanding with Matplotlib. The first is a figure, and the second is an axis to create subplots on different axes. The subplots() method helps create multiple plots in a single window or figure, and this method returns two things. Use the 'seabourn' style for these plots to look slightly better. import matplotlib.pyplot as pltĪfter importing matplotlib.pyplot. To create plots, we will import matplotlib.pyplot the alias as "plt" using the following code. It happens multiple times when you are trying to plot multiple subplots, so you find it very difficult to work with loops and automate plotting multiple plots. Create a Plot Using the subplots() Method in Matplotlib In this tutorial, you’ll learn to work with the subplot functionality provided with the matplotlib package to create and display multiple plots in one figure in Python. Many people prefer to create their plots this way, even if they only create a single plot. If you want additional plots or work with more object-oriented plots, it is best to create your plots with the subplots method. Create Multiple Subplots in One Figure in Matplotlib.Create Two Subplots in One Direction With Matplotlib.Create a Plot Using the subplots() Method in Matplotlib.Here we discuss the Introduction of Matplotlib Subplots and its different Examples as well as their input and output. It helps us in understanding any relation between various functions or variables in our data by drawing them all in one figure. Subplots function become very handy when we are trying to create multiple plots in a single figure. So, our output has 4 subplots, arranged in the order we declared in the input. This is how our input and output will look like in python. Plt.plot(a, b, label = '2nd Straight line', color = 'red')Ĩ. Plt.plot(a, b, label = '1st Straight line') Plt.plot(x, z, label = "cos function", c = 'red')

(Here we have passed 2, 2 as first two numbers to get a 2 x 2 grid) Next, we will learn to create 4 subplots, for which we will need 2 x 2 grid.Ī = ī = Ĭ = This is how our input and output will look like in python.Īs we can see in our output, we have got 2 plots stacked vertically (a 2 x 1 grid).ħ.

(Here we have passed 2, 1 as first two numbers to get a 2 x 1 grid) Let us take 2 functions, sine and cosine for this example Next, let us learn how can we place these plots vertically.įor this, we will have to create a 2 x 1 grid. Also, the subplots have taken respective positions as per our argument.ĥ. Plt.plot(c,d, label = "cos function", c = 'red')Īs we can see, our output has 2 plots placed in 1 row and 2 columns as per our input argument. This is how our input and output will look like in python: Plt.plot(c,d, label = "cos function", c = 'red') # Now the 3rd number will define the position of the 2 plots. So, in our example, the first 2 numbers are telling that we will have a grid of 1 row and 2 columns. The first 2 numbers passed in the argument define the dimensions of the grid in which we want our plots to be displayed. (Let us understand what exactly the function subplot(1, 2, 1) is doing. Let us take 2 functions, sine and cosine for this example.Ģ.
