## How to plot multiple subplots using python

python plot multiple graphs in one figure

python multiple plots in one script

python multiple plots in one figure loop

matplotlib multiple plots same figure

python plot multiple figures

seaborn multiple plots

python subplot size

I have a problem where i am given the an image and have to recreate this image using python and matplotlib, sklearn, numpy. Following is the image:

Here is the code i have written so far in python:

import matplotlib.pyplot as plt from sklearn.datasets import load_iris import numpy as np iris = load_iris() print(type(iris)) print(iris['target_names']) print(iris['data']) print(iris['target']) print(iris['DESCR']) print(iris['feature_names']) fig = plt.figure() ax1 = plt.subplot(2, 1, 1) ax2 = plt.subplot(2, 1, 2) iris = load_iris() data = np.array(iris['data']) targets = np.array(iris['target']) cd = {0: 'r', 1: 'b', 2: 'g'} cols = np.array([cd[target] for target in targets]) ax1.scatter(data[:, 0], data[:, 1], c=cols) ax2.scatter(data[:, 0], data[:, 2], c=cols) plt.show()

I am completely lost and really need help to get past this one, i only get the first 2 subplots right. Any advice would be very helpful as i have been trying to figure this one out a few days now.

This hopefully explains exactly how to create the requested images:

import matplotlib.pyplot as plt from sklearn.datasets import load_iris import numpy as np fig, subs = plt.subplots(4,3) #setting the shape of the figure in one line as opposed to creating 12 variables iris = load_iris() ##code as per the example data = np.array(iris['data']) targets = np.array(iris['target']) cd = {0:'r',1:'b',2:"g"} cols = np.array([cd[target] for target in targets]) # Row 1 subs[0][0].scatter(data[:,0], data[:,1], c=cols) subs[0][1].scatter(data[:,0], data[:,2], c=cols) subs[0][2].scatter(data[:,0], data[:,3], c=cols) # Row 2 subs[1][0].scatter(data[:,1], data[:,0], c=cols) subs[1][1].scatter(data[:,1], data[:,2], c=cols) subs[1][2].scatter(data[:,1], data[:,3], c=cols) # Row 3 subs[2][0].scatter(data[:,2], data[:,0], c=cols) subs[2][1].scatter(data[:,2], data[:,1], c=cols) subs[2][2].scatter(data[:,2], data[:,3], c=cols) #Row 4 subs[3][0].scatter(data[:,3], data[:,0], c=cols) subs[3][1].scatter(data[:,3], data[:,1], c=cols) subs[3][2].scatter(data[:,3], data[:,2], c=cols) plt.show()

**Creating multiple subplots using plt.subplots,** In this section we'll explore four routines for creating subplots in Matplotlib. figure. plt.axes also takes an optional argument that is a list of four numbers in the from matplotlib import pyplot as plt import numpy as np x = np.linspace(-5, 5, 10) y = np.random.rand(10) fig, ax = plt.subplots(nrows=4, ncols=3, figsize=(8, 6)) # ax is a 2d array with shape (4, 3), it can be sliced just like a numpy array for row in range(4): for col in range(3): ax[row][col].scatter(x, y, c='color you want') plt.show()

One way to get a figure with according sublots would be

fig, subs = plt.subplots(4,3)

subs is then a 2d array of the ares, so you can do:

subs[0][0].scatter(x,y)

**Multiple Subplots,** There are several ways to do it. The subplots method creates the figure along with the subplots that are then stored in the ax array. For example The first two optional arguments of pyplot.subplots define the number of rows and columns of the subplot grid. When stacking in one direction only, the returned axs is a 1D numpy array containing the list of created Axes. fig, axs = plt.subplots(2) fig.suptitle('Vertically stacked subplots') axs.plot(x, y) axs.plot(x, -y)

Here is an example

from matplotlib import pyplot as plt import numpy as np x = np.linspace(-5, 5, 10) y = np.random.rand(10) fig, ax = plt.subplots(nrows=4, ncols=3, figsize=(8, 6)) # ax is a 2d array with shape (4, 3), it can be sliced just like a numpy array for row in range(4): for col in range(3): ax[row][col].scatter(x, y, c='color you want') plt.show()

**How do I get multiple subplots in matplotlib?,** Matplotlib Tutorial: Subplots. multiple plots. We have given so far lots of examples for plotting graphs in the previous chapters of our Python tutorial on Matplotlib. To go beyond a regular grid to subplots that span multiple rows and columns, plt.GridSpec() is the best tool. The plt.GridSpec() object does not create a plot by itself; it is simply a convenient interface that is recognized by the plt.subplot() command. For example, a gridspec for a grid of two rows and three columns with some specified width and height space looks like this:

**Numerical & Scientific Computing with Python: Creating Subplots ,** A figure can have more than one subplot. Earlier, you learned that you can obtain your figure and axes objects by calling plt.subplots() and passing in a figure Click hereto download the full example code. Multiple subplots¶. Simple demo with multiple subplots. importnumpyasnpimportmatplotlib.pyplotaspltx1=np.linspace(0.0,5.0)x2=np.linspace(0.0,2.0)y1=np.cos(2*np.pi*x1)*np.exp(-x1)y2=np.cos(2*np.pi*x2)plt.subplot(2,1,1)plt.plot(x1,y1,'o-')plt.title('A tale of 2 subplots')plt.ylabel('Damped oscillation')plt.subplot(2,1,2)plt.plot(x2,y2,'.-')plt.xlabel('time (s)')plt.ylabel('Undamped')plt.show()

**Working With Multiple Subplots – Real Python,** Scatterplot of SepalLength Vs SepalWidth (Iris Dataset). Importing libraries; Plot a basic graph; Add title, x-axis & y-axis labels; Resize the plot. The quick and easy way; set the x and y limits in each plot to what you want. plt.xlim(60,200) plt.ylim(60,200) (for example). Just paste those two lines just before both plt.show() and they'll be the same. The harder, but better way and this is using subplots.

**Matplotlib,** Subplots combine multiple plots into a single frame. The key to using subplots is to decide Duration: 6:22
Posted: 11 May 2016 fig, ax = plt.subplots(nrows=6,ncols=6,figsize=(20, 20))fig.subplots_adjust(hspace=.5,wspace=0.4)plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)for x in range(1,32): plt.subplot(6,6,x) plt.title('day='+str(x)) plt.scatter(x1,y1) plt.scatter(x2,y2) plt.colorbar().set_label('Distance from ocean',rotation=270)plt.savefig('Plots/everyday_D color.png') plt.close()

##### Comments

- Check out seaborn, It is used for plotting data and presenting it similar to your provided image
- Use subplots
- "i only get the first 2 subplots right" You are creating only 2 subplots, how many of them do you want to get right?
- Note that there are 2 features being used for each subplot. i.e.The first 2 subplots came from our example of plotting the iris dataset. This is a permutation problem. From 4 features choose 2 which equals 12 choices in total. That is why there are 12 subplots in total. That is the only bit of advice i get to help me, so 12 in total
- loved using seaborn's pairs, thanks @AbishekAditya
- @Wynand Roberts any questions?
- Thanks allot, no Questions