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') )