HOW TO DRAW SCATTER PLOT AND BAR PLOT IN 3D

Photo by Perminder Klair on Unsplash

From day one, visualising data is what I have been mostly doing in my journey so far in data science. Initially, all the plots I drew were in two dimensional. The feeling of bringing numbers into visualisations with coding is just unmatched to any other but as the complexity of problems started escalating and data began becoming too abstract. I had this realisation that I couldn’t connect to my 2D plots because they were not personally appealing to me anymore.

The more you personalise, the better it becomes. I believe, everything in this world is a piece of art, how much it resembles you is what makes its merit. Just learning how to draw scatter-plot and bar-plot in 3D has made things interesting all over again.




2D plot

We all are familiar with this kind of two-dimensional plot. It is excellent and super comfortable, but it lacks the touch of being personal. Let’s take a look at a 3D plot.

intro 3D

Everyone uses Matplotlib for visualising data in 2D, but only a few know that it can be put to use for plotting in 3D too. Though functions would be different and the way everything is executed is different, but we don’t have to install another package and learn about it from scratch.




Toolkits

Toolkits are collections of application-specific function that extends Matplotlib. mpl_toolkit.mplot3d is a toolkit that provides some essential 3D plotting tools like scatter, line, mesh, bar-plot, etc.

from mpl_toolkits.mplot3d import Axes3D

Axes3D is an object which enables plotting in 3D. It uses keyword projection=3D. We create a matplotlib.figure.figure object and add Axes3d to it.

fig = plt.figure()
ax=fig.add_subplot(111,projection='3d')




 scatter plot3d plot
x=[1,2,3,4,5,6,7,8,9,10]
y=[5,6,2,3,13,4,1,2,4,8]
z=[5,7,6,4,3,8,0,9,6,10]

ax.set_xlabel('x_axis')
ax.set_ylabel('y_axis')
ax.set_zlabel('z_axis')

For demonstration purpose, let’s create three simple lists for x,y and z each having length of ten. If you want to plot a 2D graph in 3D make all the elements in the z list as 0.

ax.scatter(x,y,z,c='r',marker='x')
plt.show()

Scatter function will plot the scatter plot using the color red and marker as x.



barplot

For drawing bar-plot too the method of importing the toolkit and creating its object is same as above.

ax.bar3d(x,y,z,dx,dy,dz)

Unlike scatter plot here are few more parameters like dx, dy and dz. These three tells the bar3D about the length, width and height. Parameters though would depend heavily on the variables which we will use to plot. For introductory purpose lets define dx, dy and dz too simple.

x=[1,2,3,4,5,6,7,8,9,10]
y=[5,6,2,3,13,4,1,2,4,8]
z=[0,0,0,0,0,0,0,0,0,0]
dx=np.ones(10)
dy=np.ones(10)
dz=[1,2,3,4,5,6,7,8,9,10]

Using our lovely numpy we have define dx and dy as list of ten elements each being 1 and dz as a list of natural number till 10.

bar plot

You might have noticed that z-list has been intentionally made up of all zeroes. If you put any number except zero in the list your graph would look something like this.




hanging 3D plot

x=[1,2,3,4,5,6,7,8,9,10]
y=[5,6,2,3,13,4,1,2,4,8]
z=[0,0,0,3,0,0,0,0,0,0]

for y[i]=3 our bar is hanging in the mid air, which is not expected from the most of us.

3D plots are easy to make and shows that you have gone one step further to personalise the plot. Make 3D plots share them with your friends, teachers, colleagues and Boss. Let me know in comments how they find your 3D graph.




 

 

 

 

 

About the author

Harsh Bhojwani

Hi, I'm Harsh Bhojwani, an aspiring blogger with an obsession for all things related to Data Science. This blog is dedicated to helping people learn Data Science.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *