Tuesday, November 3, 2015

Menggunakan Matplotlib Bagian 1



Untuk belajar membuat grafik dengan matplotlib dapat menggunakan contoh-contoh grafik yang sudah dibuat yang tersedia di web mplotlib http://matplotlib.org/gallery.html. Dari web tersebut kita dapat belajar membuat grafik dengan menggunakan contoh.

Line Plot

from numpy import *
import matplotlib.pyplot as plt

x = linspace(0,2 * pi, 50)

  
plt.plot(x, sin(x), 'r-^', x, sin(2*x), 'b-o')
plt.show()


Output:


Simple Scatter

from numpy import *
import matplotlib.pyplot as plt

x = linspace(0,2 * pi, 50)
  
plt.scatter(x, sin(x))
plt.show()

Output:
Colormapped Scatter

from numpy import *
import matplotlib.pyplot as plt

x = random.random_sample(200)

y = random.random_sample(200)

size = random.random_sample(200*30)

color = random.random_sample(200)
   
plt.scatter(x, y, size, color)
plt.colorbar()
plt.show()

Output
Multiple Figure

from numpy import *
import matplotlib.pyplot as plt

t = linspace(0, 2*pi, 50)

x = sin(t)
y = cos(t)

#create figure
plt.figure()
plt.plot(x)


plt.figure()
plt.plot(y)
plt.show()


Output:

Multiple Plots Using subplot

from numpy import *
import matplotlib.pyplot as plt

x = array([1,2,3,2,1])

y = array([1,3,2,3,1])

#to divide the ploting area
plt.subplot(2, 1, 1) #2= rows, 2=columns, 1 active plot
plt.plot(x)

#activate now plot
plt.subplot(2, 1, 2)
plt.plot(y)

plt.show()

Output:


Dari web https://matematiku.wordpress.com/category/programming/ berikut membuat gambar hati dari persamaan matematik.

import matplotlib.pylab as plt
import numpy as np

x = np.linspace(-2,2,1000)
y1 = np.sqrt(1-(abs(x)-1)**2)
y2 = -3*np.sqrt(1-(abs(x)/2)**0.5)
plt.fill_between(x, y1, color='pink')
plt.fill_between(x, y2, color='pink')
plt.xlim([-2.5, 2.5])
plt.ylim([-3.5, 1.5])
plt.text(0, -0.4, 'Heart For You', fontsize=20, fontweight='bold',
           color='blue', horizontalalignment='center')
plt.text(0, -1.0, r'$y_1=\sqrt{1-(|x|-1)^2}$', fontsize=16,
           horizontalalignment='center')
plt.text(0, -1.5, r'$y_2=-3\sqrt{1-\left(\frac{|x|}{2}\right)^{\frac{1}{2}}}$', fontsize=16, horizontalalignment='center')
plt.show()

Output:
Sekian dan terimakasih. @wawanhn


Previous Post
Next Post

0 comments: