Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

Wednesday, May 22, 2013

Adding a colorbar to a plot


# Create the maps in a subset of the figure: we leave space for the colorbar v = viz.plot_map(data, affine, axes=[0., 0., 0.9, 1.])
# Create a dedicated Axis for the colorbar cax = v.axes['z'].ax.get_figure().
add_axes([0.9, 0.1, 0.03, 0.8])
# Add the colorbar relatively to the "seconde layer" of the last map colorbar(v.axes['z'].ax.
images[1], cax=cax)



To correctly position the colorbar you can first create an axis/figure that you pass as plot_map arguments.

fig = pl.figure(figsize=(6, 3), facecolor='w', edgecolor='w')
ax = pl.axes([.02, .0, .83, .95], axisbg='w')
slicer = plot_map(tag_map, affine, title=title, cmap=pl.cm.hot,
                      vmin=-vmax, vmax=vmax, figure=fig, axes=ax)
cax = slicer.axes['z'].ax.get_figure().add_axes([.87, .1, .03, .8])
pl.colorbar(slicer.axes['x'].ax.images[1], cax=cax)

Friday, April 20, 2012

Informative features

Let's say you want to generate a synthetic data-set to play around with for classification,
and you set

n_samples = 100
n_features = 1000



and you generate the following data
import numpy as np
import matplotlib.pyplot as plt
X1 = np.asarray(np.randn(n_samples/2, n_features))
X2 = np.asarray(np.randn(n_samples/2, n_features)) + 5
X = np.append(X1, X2, axis=0)
rnd.shuffle(X)

plt.scatter(X[:,0], X[:,1])
plt.show()




For a binary classification, the function which determines our labels is \[y = sign(X \bullet \omega)\]
Where \(\omega\) is our coefficients.
For now, let's set our coefficients equal to a bunch of zeros:
coef = (np.zeros(n_features))
If we wish to make it so that we have, say, 10 informative features, we can for example set 10 of our coefficients equal to a non-zero value. Now when we dot it with our data, X, we will basically
tell it that the 10 non-zero coefficients are our informative features, while the rest that will be
multiplied by zeros are not informative.

So,

coef[:10] = 1
y = np.sign(np.dot(X,coef))


will give us our corresponding labels such that we have 10 informative features.
A way to visualise this, is to use the Scikit-Learn package's f_classif function.
If you have the Scikit-learn package installed, do the following:

from sklearn.feature_selection import f_classif
p,v = f_classif(X,y)
plt.plot(p)
plt.show()



Here you can see that the first 10 features are rated as the most informative.

Thursday, February 23, 2012

Removing tick-labels from an axis in Matplotlib

If you want a make a 3D plot for which you don't want any text on the axis-ticks,
the following should do that (pl = pylab):


fig = pl.figure(1, figsize=(4, 3))
pl.clf()
ax = Axes3D(fig, elev=43.5, azim=-110)
....
....
ax.set_xlabel('X_1')
ax.set_ylabel('X_2')
ax.set_zlabel('Y')
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])

An example plot:



for python with pylab, matplotlib & Axes3D