Thursday, May 9, 2013

PL#11: 3D plot surface plot


from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
Xv = np.arange(-5, 5, 0.05)
Yv = np.arange(-5, 5, 0.05)
Xv, Yv = np.meshgrid(Xv, Yv)
R = np.sqrt(Xv**2 + Yv**2)
Zv = np.sin(R)
surf = ax.plot_surface(Xv, Yv, Zv, rstride=1, cstride=1, cmap=plt.cm.jet,
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()




PL#10: Wire frame plot


from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(-10, 10, 0.05)
Y = np.arange(-10, 10, 0.05)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()


PL#9: scatter (point) plot

import numpy as np
from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

# data
x = [1,4,8,    9, 10, 12,  18, 21]
y = [1, 3, 8,  10, 21, 34, 7, 32]
z1 = [4, 6, 8,  23, 19, 2 , 0, 9]
z2 = [10, 5, 2, 11, 18, 8,  11, 15]
z3 = [6, 15, 12, 1, 8, 16,  7, 12]

# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z1, c='r', marker='o')
ax.scatter(x, y, z2, c='g', marker='v')
ax.scatter(x, y, z3, c='b', marker='+')
 
 

PL#8: XY scatter plot large number of points


import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# data - random for example
seed (123)
x = np.random.randn(100000)
y = np.random.randn(100000)

# pixel marker
plot (x, y, ",")


import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# data - random for example
seed (123)
x = np.random.randn(100000)
y = np.random.randn(100000)

out = hist2d(x,y,bins=100)




import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# data - random for example
seed (123)
x = np.random.randn(100000)
y = np.random.randn(100000)

heatm, xedg, yedg = np.histogram2d(x, y, bins=100)
extent = [xedg[0], xedg[-1], yedg[0], yedg[-1]]

plt.clf()
#heatmap
plt.imshow(heatm, extent=extent)
plt.show()




PL#7: Pie chart in python


#! /usr/local/bin/python

from pylab import *

figure(1, figsize=(4,4))
axes([0.1, 0.1, 0.8, 0.8])

pievar =[60, 30, 20, 10, 5, 2, 3]
pie(pievar)


# with labels, user defined colors
#! /usr/local/bin/python
from pylab import *
figure(1, figsize=(4,4))
axes([0.1, 0.1, 0.8, 0.8])

pievar =[60, 30, 20, 10, 5, 2, 3]
mylab = ["A", "B", "C", "d", "e", "f", "G"]
#http://pythonhosted.org/ete2/reference/reference_svgcolors.html
mycol = ["crimson", "SeaGreen","green", "cyan", "blue", "gray", "pink"]
pie(pievar, labels = mylab, colors = mycol)



Wednesday, May 8, 2013

PL#6: Plot bar plot (error bar plot) with different width scaled to other variable


#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight1 = (25, 39, 60, 35, 27, 5, 8, 12)
widthvar = (15, 29, 30, 15, 17, 25, 18, 8)
width = [x/float (max(widthvar)) for x in widthvar]

ind = np.arange(N) # the x locations
rects = plt.bar(ind, barheight1, width = width, color='g', yerr=berr1, error_kw=dict(elinewidth=2, ecolor='pink'))
plt.axis([-0.3, 8, -10, 70])
plt.grid(True)

# other labs
plt.ylabel('Ylab')
plt.title('My plot title')
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8') )




PL#5: Grouped bar and error bars


#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight1 = (25, 39, 60, 35, 27, 5, 8, 12)
barheight2 = (15, 29, 30, 15, 17, 25, 18, 8)


width = 0.35
ind = np.arange(N) # the x locations
rects1 = plt.bar(ind, barheight1, width = width, color='g')
rects2 = plt.bar(ind+width, barheight2, width = width, color='y')
plt.axis([-0.3, 8, 0, 65])
plt.grid(True)

# other labs
plt.ylabel('Ylab')
plt.title('My plot title')
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8') )

plt.legend( (rects1[0], rects2[0]), ('CLS1', 'CLS2') )


#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight1 = (25, 39, 60, 35, 27, 5, 8, 12)
barheight2 = (15, 29, 30, 15, 17, 25, 18, 8)
berr1 = [8]*len(barheight1)
berr2 = [6]*len(barheight2)

width = 0.35
ind = np.arange(N) # the x locations
rects1 = plt.bar(ind, barheight1, width = width, color='g', yerr=berr1, error_kw=dict(elinewidth=2, ecolor='pink'))

rects2 = plt.bar(ind+width, barheight2, width = width, color='y', yerr=berr2, error_kw=dict(elinewidth=2, ecolor='pink'))
plt.axis([-0.3, 8, 0, 65])
plt.grid(True)

# other labs
plt.ylabel('Ylab')
plt.title('My plot title')
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8') )

plt.legend( (rects1[0], rects2[0]), ('CLS1', 'CLS2') )








PL#4: Bar and error bar plot


#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight = (25, 39, 60, 35, 27, 65, 8, 12)

ind = np.arange(N) # the x locations
rects1 = plt.bar(ind, barheight, width = 0.6, color='g')

plt.grid(True)


#!/usr/bin/env python
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight = (25, 39, 60, 35, 27, 65, 8, 12)

ind = np.arange(N) # the x locations
rects1 = plt.bar(ind, barheight, width = 1, color='g')

plt.grid(True)





#!/usr/bin/env python

# add error bar 
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt

N = 8
barheight = (25, 39, 60, 35, 27, 65, 8, 12)
errbar = (6, 8, 3, 4, 8,4, 3, 8)
ind = np.arange(N) # the x locations
rects1 = plt.bar(ind, barheight, width = 0.6, color='g', yerr=errbar, error_kw=dict(elinewidth=3, ecolor='yellow'))

plt.grid(True)








PL#3: Histogram with density line


#!/usr/bin/env python
from __future__ import print_function


import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

mu = 50
sigma = 8
xv = mu + sigma*np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(xv, 50, normed=1, facecolor='blue', alpha=0.65)

# add a density line
yline = mlab.normpdf( bins, mu, sigma)
l = plt.plot(bins, yline, 'r--', linewidth=2)

plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=50,\ \sigma=8$')
plt.axis([15, 85, 0, 0.06])
plt.grid(True)










PL#2: XY scatter plot with multiple data series


#!/usr/bin/env python

from __future__ import print_function

from pylab import *

x = array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
y1 = array(3 + x*0.5 + np.random.rand(len(x) ))
y2 = array(3 - x*0.6 + np.random.rand(len(x) ))

def fit1(x):
      return 3+0.5*x
xfit = array( [amin(x), amax(x) ] )

plot(x,y1,'ks', xfit, fit1(xfit), 'r-', lw=2, marker='o',
markerfacecolor='blue', markersize=10)


xlabel('x-axis')
ylabel('y-axis')
title(r'regression plots')






# add second data series 
def fit2(x):
     return 5 - 0.6*x

plot(x,y2,'ks', xfit, fit2(xfit), 'r-', lw=2, marker='v',
markerfacecolor='green', markersize=10)
setp(gca(), yticks=(-8,-6,-4, -2, 0,2, 4, 6, 8, 10), xticks=(2,4,6, 8,10, 12, 14, 16))
text(14.5,11, 'eq-I', fontsize=10, color = "blue" )
text(14.5,-2, 'eq-II', fontsize=10, color = "green" )




PL#1: Line and scatter plot



from pylab import *
x=arange(0,10,0.03)
seed(123)
y= 0.6 * x + np.random.rand(len(x))
plot(x,y) # plot with default line and color
xlabel('x-axis')
ylabel('y-axis')
title(r'my plot')
show()




from pylab import *
x=arange(0,10,0.03)
y= 0.6 * x + np.random.rand(len(x))
plot(x,y, ".") # plot with default line and color
xlabel('x-axis')
ylabel('y-axis')
title(r'my plot')
show()


subplot(12)


x=arange(0,10,0.03)
seed(123)
y= 0.6 * x + np.random.rand(len(x))
plot(x,y, "ro") # red circle
xlabel('x-axis')
ylabel('y-axis')
title(r'my plot')
show()