Scatter Plot
Get data from a database table and plot in a 2d figure.
import pyodbc
import numpy as np
import matplotlib.pyplot as plt
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=111.111.111.111;DATABASE=your database;Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT Label, feature1, feature2 FROM table')
rows = cursor.fetchall()
rows_array = np.array(rows)
Y = rows_array[:,0] #first column is the label
Y = np.reshape(Y, (1, len(Y)))[0] #convert to a row vector
X = rows_array[:, 1:]
x_min, x_max = X[:,0].min() - .5, X[:,0].max() + 0.5
y_min, y_max = X[:,1].min() - .5, X[:,1].max() + 0.5
plt.figure(figsize=(8,8))
plt.clf()
plt.scatter(X[:,0], X[:,1], c=Y)
plt.xlabel('feature1')
plt.ylabel('feature2')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
3D plot needs a different library.
The following can be added to the above codes. The elev and azim ajusts the observing angles. the cmap chooses the color scheme. there are existing schemas in plt.cm.
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(1, figsize=(8, 8))
ax = Axes3D(fig, elev=-150, azim=120)
ax.scatter(X[:, 1], X[:, 2], X[:, 3], c=y, cmap = plt.cm.cool)
plt.show()