3Dimension Plotting in Python

3Dimension Plotting in Python

Series: Python for kids

In our endavor to make learning fun and free of memorization, we have been using Python to allow children to experiment and analyze data to form their own opinion and discover facts. This way, children engage is learning process with more intensity and the they show higher retention rate vis a vis conventional learning.

This makes them think and try different things before they get it right. The focus shifts from being always ‘right’ to get it right after several failed attempts. In this part of the series

Children create simple shapes using markers, move marker left, right, front, back, up and down. They learn coordinates geometry where the position of points on the 3D plane.

In this blog we are using Matplotlibn library. Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy.

Import libraries

The Matplotlib Jupyter Widget Backend

ipympl enables the interactive features of matplotlib in the Jupyter notebook and in JupyterLab. To enable the ipympl backend, simply use the matplotlib Jupyter magic: # %matplotlib widget

In [1]:
import matplotlib.pyplot as plt
import numpy as np
# %matplotlib widget

Plot a point at origin(0, 0, 0) in 3D

In [2]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '*')

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\2866066107.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Change color of point

In [3]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'green', marker = 'o')

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\1351514269.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Make it bigger point

In [4]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', 
        marker = 'o', markersize = 20)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\1314132036.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Create different shape like triangle in place of point

In [5]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 20)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\2692021183.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Create points left, right, front, back, up and down from the origin

Left and right points

In [6]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)
ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\2614721598.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Front and back points

In [7]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\2987012512.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Up and down points

In [8]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

ax.plot(0, 0, 1, color = 'cyan', marker = 'o', markersize = 10)
ax.plot(0, 0, -1, color = 'black', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\3695728694.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Add annotation for origin

In [9]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)
ax.text(0 + 0.03, 0 + 0.01, 0 + 0.01, s = 'O', fontsize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

ax.plot(0, 0, 1, color = 'cyan', marker = 'o', markersize = 10)
ax.plot(0, 0, -1, color = 'black', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\3042329124.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Connect left and right point

In [10]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)
ax.text(0 + 0.03, 0 + 0.01, 0 + 0.01, s = 'O', fontsize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(
    [1, -1],
    [0, 0],
    [0, 0]
)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

ax.plot(0, 0, 1, color = 'cyan', marker = 'o', markersize = 10)
ax.plot(0, 0, -1, color = 'black', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\3211651894.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Connect front and back point

In [11]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)
ax.text(0 + 0.03, 0 + 0.01, 0 + 0.01, s = 'O', fontsize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(
    [1, -1],
    [0, 0],
    [0, 0]
)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

ax.plot(
    [0, 0],
    [1, -1],
    [0, 0]
)

ax.plot(0, 0, 1, color = 'cyan', marker = 'o', markersize = 10)
ax.plot(0, 0, -1, color = 'black', marker = 'o', markersize = 10)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\1746733648.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Connect up and down point

In [12]:
fig = plt.figure(figsize = (5, 5))
ax = plt.axes(projection='3d')

ax.plot(0, 0, 0, color = 'red', marker = '^', markersize = 10)
ax.text(0 + 0.03, 0 + 0.01, 0 + 0.01, s = 'O', fontsize = 10)

ax.plot(1, 0, 0, color = 'green', marker = 'o', markersize = 10)
ax.plot(-1, 0, 0, color = 'blue', marker = 'o', markersize = 10)

ax.plot(
    [1, -1],
    [0, 0],
    [0, 0]
)

ax.plot(0, 1, 0, color = 'yellow', marker = 'o', markersize = 10)
ax.plot(0, -1, 0, color = 'purple', marker = 'o', markersize = 10)

ax.plot(
    [0, 0],
    [1, -1],
    [0, 0]
)

ax.plot(0, 0, 1, color = 'cyan', marker = 'o', markersize = 10)
ax.plot(0, 0, -1, color = 'black', marker = 'o', markersize = 10)

ax.plot(
    [0, 0],
    [0, 0],
    [1, -1]
)

plt.show()
C:\Users\Nutan\AppData\Local\Temp\ipykernel_1144\838292702.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = plt.axes(projection='3d')
Figure

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing