View Tutorial Metadata Edit Content Revision History Add to Watchlist Add New Tutorial 3d draw with Python Matplotlib

3d draw with Python Matplotlib

Introduction

Matplotlib is a python library that allows easy drawing for series of numerical data in two or three dimensions. Python and matplotlib are very easy to install on windows or other OS-es.

The code

If you are new to Python, be careful, indentation matters.

#!/bin/env python

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#the function to generate the pythgorean numbers(yield is a kind of return)
def pythagora(max):
    a = 1
    while(a <= max):
        b = a+1
        while (b <= max):
            yield (b ** 2  - a ** 2) , (2 * a * b) , (a ** 2 + b ** 2)
            b += 1
        a += 1

fig = plt.figure()

ax = Axes3D(fig)
#in some versions may be: ax = fig.add_subplot(111, projection='3d')

#the arrays to hold the coordinates
x = []
y = []
z = []

#here is the iterator in action
for li in pythagora(15):
    x.append(li[0])
    y.append(li[1])
    z.append(li[2])

#the optional s and c are for dimension and color of the dots
ax.scatter( x , y, z, s=5, c='#00688B' )

#this is another type of graphic with lines:
#ax.plot( x , y,  z, c='c')

plt.show()

Only plain text supported.

Optional

Required - will be kept private

Optional

 
 

Rating: (0+, 0-) In: Programming, Python