The mplot3d toolkit allows us to extend the capabilities of visualization to 3D data. It is included with all standard installations of matplotlib.
Usually if the figure is displayed in a separate window, we can rotate the axes of the three-dimensional representation with the mouse but with this package we are still using the Figure object, but instead of the Axes object we will define a new kind of object, called Axes3D introduced by this toolkit.
Thus, to use the Axes3D object, we need to add a new import to the code
from mpl_toolkits.mplot3d import Axes3D
Using the mplot3D package, surfaces can be drawn directly in 3D. See the following program :
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-2,2,0.1)
Y = np.arange(-2,2,0.1)
X,Y = np.meshgrid(X,Y)
def f(x,y):
return (1 - y**5 + x**5)*np.exp(-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1)
plt.show()
In the above program we use the same function z = f (x, y) which we have used in the contour map in the previous post. First we calculate the meshgrid, then we view the surface using the plot_
surface() function as shown in the output below:
We can change the colormap by setting the cmap kwarg. We can also rotate the surface using the view_init() function which adjusts the view point from which we see the surface, changing the two kwargs called elev and azim. Through their combination we can get the surface displayed from any angle. The first kwarg adjusts the height at which the surface is seen, while azim adjusts the angle of rotation of the surface. The following program change the color map using plt.cm.hot and moves the view point to elev=30 and azim=125:
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-2,2,0.1)
Y = np.arange(-2,2,0.1)
X,Y = np.meshgrid(X,Y)
def f(x,y):
return (1 - y**5 + x**5)*np.exp(-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1, cmap=plt.cm.hot)
ax.view_init(elev=30,azim=125)
plt.show()
The output of the program is shown below:
Scatter Plots in 3D are the most used among all 3D views as with this type of visualization, we can identify if the points follow particular trends and if they tend to cluster. In the following program we will use the scatter() function as the 2D case but applied on the Axes3D object. By doing this, we can visualize different series, expressed by the calls to the scatter() function, all together in the same 3D representation:
xs = np.random.randint(30,40,100)
ys = np.random.randint(20,30,100)
zs = np.random.randint(10,20,100)
xs2 = np.random.randint(50,60,100)
ys2 = np.random.randint(30,40,100)
zs2 = np.random.randint(50,70,100)
xs3 = np.random.randint(10,30,100)
ys3 = np.random.randint(40,50,100)
zs3 = np.random.randint(40,50,100)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xs,ys,zs)
ax.scatter(xs2,ys2,zs2,c='r',marker='^')
ax.scatter(xs3,ys3,zs3,c='g',marker='*')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
The output of the program is shown below:
Bar Charts in 3D
Bar Charts in 3D are widely used in data analysis. Here we use the bar() function applied to the object Axes3D. Even if we define multiple series, we can accumulate several calls to the bar() function in the same 3D visualization as shown in the following program :
x = np.arange(8)
y = np.random.randint(0,10,8)
y2 = y + np.random.randint(0,3,8)
y3 = y2 + np.random.randint(0,3,8)
y4 = y3 + np.random.randint(0,3,8)
y5 = y4 + np.random.randint(0,3,8)
clr = ['#4bb2c5', '#c5b47f', '#EAA228', '#579575', '#839557',
'#958c12', '#953579', '#4b5de4']
fig = plt.figure()
ax = Axes3D(fig)
ax.bar(x,y,0,zdir='y',color=clr)
ax.bar(x,y2,10,zdir='y',color=clr)
ax.bar(x,y3,20,zdir='y',color=clr)
ax.bar(x,y4,30,zdir='y',color=clr)
ax.bar(x,y5,40,zdir='y',color=clr)
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.view_init(elev=40)
plt.show()
The output of the program is shown below:
Multi-Panel Plots
The Multi-Panel Plots provides the ability to view charts within others, enclosed within frames. Since we are talking of frames, i.e., Axes objects, we need to separate the main Axes (i.e., the general chart) from the frame we want to add that will be another instance of Axes. To do this, we use the figures() function to get the Figure object on which we will define two different Axes objects using the
add_axes() function as shown in the following program :
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])
plt.show()
The output of the program is shown below:
Now let's fill the previous Axes with real data, as shown in the following program :
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])
x1 = np.arange(10)
y1 = np.array([1,2,7,1,5,2,4,2,3,1])
x2 = np.arange(10)
y2 = np.array([1,3,4,5,4,5,2,6,4,3])
ax.plot(x1,y1)
inner_ax.plot(x2,y2)
plt.show()
The output of the program is shown below which is a more realistic visualization of a subplot within another plot:
matplotlib allows us to manage even more complex cases using another function called GridSpec(). This subdivision allows splitting the drawing area into a grid of sub-areas, to which we can assign one or more of them to each subplot, so that in the end we can obtain subplots with different sizes and orientations. See the following program :
gs = plt.GridSpec(3,3)
fig = plt.figure(figsize=(6,6))
fig.add_subplot(gs[1,:2])
fig.add_subplot(gs[0,:2])
fig.add_subplot(gs[2,0])
fig.add_subplot(gs[:2,2])
fig.add_subplot(gs[2,1:])
plt.show()
The output of the program is shown below:
Now let's how we can use these subplots. See the following program :
gs = plt.GridSpec(3,3)
fig = plt.figure(figsize=(6,6))
x = np.arange(8)
y = np.random.randint(0,10,8)
x1 = np.array([1,3,2,5])
y1 = np.array([4,3,7,2])
x2 = np.arange(5)
y2 = np.array([3,2,4,6,4])
s1=fig.add_subplot(gs[1,:2])
s1.plot(x,y,'r')
s2=fig.add_subplot(gs[0,:2])
s2.bar(x2,y2)
s3=fig.add_subplot(gs[2,0])
s3.barh(x2,y2,color='g')
s4=fig.add_subplot(gs[:2,2])
s4.plot(x2,y2,'k')
s5=fig.add_subplot(gs[2,1:])
s5.plot(x1,y1,'b^',x2,y2,'yo')
plt.show()
In the above program we have used the Axes object returned by each add_subplot() function to call the plot() function to draw the corresponding plot. The output of the program is shown below which is a grid of subplots which can display many plots at the same time:
Here I am ending today’s post. Now we know enough about data visualization and ready to move on. Until we meet again to start with Machine Learning with scikit-learn, keep practicing and learning Python, as Python is easy to learn!
Usually if the figure is displayed in a separate window, we can rotate the axes of the three-dimensional representation with the mouse but with this package we are still using the Figure object, but instead of the Axes object we will define a new kind of object, called Axes3D introduced by this toolkit.
Thus, to use the Axes3D object, we need to add a new import to the code
from mpl_toolkits.mplot3d import Axes3D
Using the mplot3D package, surfaces can be drawn directly in 3D. See the following program :
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-2,2,0.1)
Y = np.arange(-2,2,0.1)
X,Y = np.meshgrid(X,Y)
def f(x,y):
return (1 - y**5 + x**5)*np.exp(-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1)
plt.show()
In the above program we use the same function z = f (x, y) which we have used in the contour map in the previous post. First we calculate the meshgrid, then we view the surface using the plot_
surface() function as shown in the output below:
We can change the colormap by setting the cmap kwarg. We can also rotate the surface using the view_init() function which adjusts the view point from which we see the surface, changing the two kwargs called elev and azim. Through their combination we can get the surface displayed from any angle. The first kwarg adjusts the height at which the surface is seen, while azim adjusts the angle of rotation of the surface. The following program change the color map using plt.cm.hot and moves the view point to elev=30 and azim=125:
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-2,2,0.1)
Y = np.arange(-2,2,0.1)
X,Y = np.meshgrid(X,Y)
def f(x,y):
return (1 - y**5 + x**5)*np.exp(-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1, cmap=plt.cm.hot)
ax.view_init(elev=30,azim=125)
plt.show()
The output of the program is shown below:
Scatter Plots in 3D are the most used among all 3D views as with this type of visualization, we can identify if the points follow particular trends and if they tend to cluster. In the following program we will use the scatter() function as the 2D case but applied on the Axes3D object. By doing this, we can visualize different series, expressed by the calls to the scatter() function, all together in the same 3D representation:
xs = np.random.randint(30,40,100)
ys = np.random.randint(20,30,100)
zs = np.random.randint(10,20,100)
xs2 = np.random.randint(50,60,100)
ys2 = np.random.randint(30,40,100)
zs2 = np.random.randint(50,70,100)
xs3 = np.random.randint(10,30,100)
ys3 = np.random.randint(40,50,100)
zs3 = np.random.randint(40,50,100)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xs,ys,zs)
ax.scatter(xs2,ys2,zs2,c='r',marker='^')
ax.scatter(xs3,ys3,zs3,c='g',marker='*')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
The output of the program is shown below:
Bar Charts in 3D
Bar Charts in 3D are widely used in data analysis. Here we use the bar() function applied to the object Axes3D. Even if we define multiple series, we can accumulate several calls to the bar() function in the same 3D visualization as shown in the following program :
x = np.arange(8)
y = np.random.randint(0,10,8)
y2 = y + np.random.randint(0,3,8)
y3 = y2 + np.random.randint(0,3,8)
y4 = y3 + np.random.randint(0,3,8)
y5 = y4 + np.random.randint(0,3,8)
clr = ['#4bb2c5', '#c5b47f', '#EAA228', '#579575', '#839557',
'#958c12', '#953579', '#4b5de4']
fig = plt.figure()
ax = Axes3D(fig)
ax.bar(x,y,0,zdir='y',color=clr)
ax.bar(x,y2,10,zdir='y',color=clr)
ax.bar(x,y3,20,zdir='y',color=clr)
ax.bar(x,y4,30,zdir='y',color=clr)
ax.bar(x,y5,40,zdir='y',color=clr)
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.view_init(elev=40)
plt.show()
The output of the program is shown below:
Multi-Panel Plots
The Multi-Panel Plots provides the ability to view charts within others, enclosed within frames. Since we are talking of frames, i.e., Axes objects, we need to separate the main Axes (i.e., the general chart) from the frame we want to add that will be another instance of Axes. To do this, we use the figures() function to get the Figure object on which we will define two different Axes objects using the
add_axes() function as shown in the following program :
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])
plt.show()
The output of the program is shown below:
Now let's fill the previous Axes with real data, as shown in the following program :
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])
x1 = np.arange(10)
y1 = np.array([1,2,7,1,5,2,4,2,3,1])
x2 = np.arange(10)
y2 = np.array([1,3,4,5,4,5,2,6,4,3])
ax.plot(x1,y1)
inner_ax.plot(x2,y2)
plt.show()
The output of the program is shown below which is a more realistic visualization of a subplot within another plot:
matplotlib allows us to manage even more complex cases using another function called GridSpec(). This subdivision allows splitting the drawing area into a grid of sub-areas, to which we can assign one or more of them to each subplot, so that in the end we can obtain subplots with different sizes and orientations. See the following program :
gs = plt.GridSpec(3,3)
fig = plt.figure(figsize=(6,6))
fig.add_subplot(gs[1,:2])
fig.add_subplot(gs[0,:2])
fig.add_subplot(gs[2,0])
fig.add_subplot(gs[:2,2])
fig.add_subplot(gs[2,1:])
plt.show()
The output of the program is shown below:
Now let's how we can use these subplots. See the following program :
gs = plt.GridSpec(3,3)
fig = plt.figure(figsize=(6,6))
x = np.arange(8)
y = np.random.randint(0,10,8)
x1 = np.array([1,3,2,5])
y1 = np.array([4,3,7,2])
x2 = np.arange(5)
y2 = np.array([3,2,4,6,4])
s1=fig.add_subplot(gs[1,:2])
s1.plot(x,y,'r')
s2=fig.add_subplot(gs[0,:2])
s2.bar(x2,y2)
s3=fig.add_subplot(gs[2,0])
s3.barh(x2,y2,color='g')
s4=fig.add_subplot(gs[:2,2])
s4.plot(x2,y2,'k')
s5=fig.add_subplot(gs[2,1:])
s5.plot(x1,y1,'b^',x2,y2,'yo')
plt.show()
In the above program we have used the Axes object returned by each add_subplot() function to call the plot() function to draw the corresponding plot. The output of the program is shown below which is a grid of subplots which can display many plots at the same time:
Here I am ending today’s post. Now we know enough about data visualization and ready to move on. Until we meet again to start with Machine Learning with scikit-learn, keep practicing and learning Python, as Python is easy to learn!
0 comments:
Post a Comment