How to plot hyperplane SVM in python? - data-visualization

My first question so please bear with me :)
I use the Shogun toolbox to work with SVM in Python. I just experiment first to get a better understanding of SVM's. I wrote something in Python with some data points to linearly separate. I use the LibSVM()
X = np.array([[2.0, 2.0, 1.0, 1.0],
[1.0, -1.0, 1.0, -1.0]])
Y = np.array([[4.0, 5.0, 5.0, 4.0],
[1.0, 1.0, -1.0, -1.0]])
After training the SVM with the given data I can retrieve its bias(get_bias()), the support vectors(get_support_vectors()) and other properties. What I can't get done is plotting the line/hyperplane. I know the equation for the hyperplane is y=wx + b but how to write/plot this down to see it in my figure.

for a complete example
import numpy as np
import matplotlib.pyplot as plt
def __intersect(rect, line):
l = []
xmin,xmax,ymin,ymax = rect
a,b,c = line
assert a!=0 or b!=0
if a == 0:
y = -c/b
if y<=ymax and y>=ymin:
l.append((xmin, y))
l.append((xmax, y))
return l
if b == 0:
x = -c/a
if x<=xmax and x>=xmin:
l.append((x, ymin))
l.append((x, ymax))
return l
k = -a/b
m = -c/b
for x in (xmin, xmax):
y = k*x+m
if y<=ymax and y>= ymin:
l.append((x,y))
k = -b/a
m = -c/a
for y in (ymin, ymax):
x = k*y+m
if x<xmax and x> xmin:
l.append((x,y))
return l
def plotline(coef, *args, **kwargs):
'''plot line: y=a*x+b or a*x+b*y+c=0'''
coef = np.float64(coef[:])
assert len(coef)==2 or len(coef)==3
if len(coef) == 2:
a, b, c = coef[0], -1., coef[1]
elif len(coef) == 3:
a, b, c = coef
ax = plt.gca()
limits = ax.axis()
points = __intersect(limits, (a,b,c))
if len(points) == 2:
pts = np.array(points)
ax.plot(pts[:,0], pts[:,1], *args, **kwargs)
ax.axis(limits)
def circle_out(x, y, s=20, *args, **kwargs):
'''Circle out points with size 's' and edgecolors'''
ax = plt.gca()
if 'edgecolors' not in kwargs:
kwargs['edgecolors'] = 'g'
ax.scatter(x, y, s, facecolors='none', *args, **kwargs)
def plotSVM(coef, support_vectors=None):
coef1 = coef[:]
coef2 = coef[:]
coef1[2] += 1
coef2[2] -= 1
plotline(coef, 'b', lw=2)
plotline(coef1, 'b', ls='dashed')
plotline(coef2, 'b', ls='dashed')
if support_vectors != None:
circle_out(support_vectors[:,0], support_vectors[:,1], s=100)
from pylab import *
X = array([[2.0, 2.0, 1.0, 1.0],
[1.0, -1.0, 1.0, -1.0]])
Y = array([[4.0, 5.0, 5.0, 4.0],
[1.0, 1.0, -1.0, -1.0]])
data = hstack((X,Y)).T
label = hstack((zeros(X.shape[1]), ones(Y.shape[1])))
from sklearn.svm import SVC
clf = SVC(kernel='linear')
clf.fit(data, label)
coef = [clf.coef_[0,0], clf.coef_[0,1], clf.intercept_[0]]
scatter(data[:,0], data[:,1], c=label)
plotSVM(coef, clf.support_vectors_)
show()

from pylab import *
def __intersect(rect, line):
l = []
xmin,xmax,ymin,ymax = rect
a,b,c = line
assert a!=0 or b!=0
if a == 0:
y = -c/b
if y<=ymax and y>=ymin:
l.append((xmin, y))
l.append((xmax, y))
return l
if b == 0:
x = -c/a
if x<=xmax and x>=xmin:
l.append((x, ymin))
l.append((x, ymax))
return l
k = -a/b
m = -c/b
for x in (xmin, xmax):
y = k*x+m
if y<=ymax and y>= ymin:
l.append((x,y))
k = -b/a
m = -c/a
for y in (ymin, ymax):
x = k*y+m
if x<=xmax and y>= xmin and len(l) < 2:
l.append((x,y))
return l
def plotLine(coef, *args, **kwargs):
'''plot line: y=a*x+b or a*x+b*y+c=0'''
coef = float64(coef[:])
assert len(coef)==2 or len(coef)==3
if len(coef) == 2:
a, b, c = coef[0], -1., coef[1]
elif len(coef) == 3:
a, b, c = coef
ax = gca()
limits = ax.axis()
print limits
points = __intersect(limits, (a,b,c))
print points
if len(points) == 2:
pts = array(points)
ax.plot(pts[:,0], pts[:,1], *args, **kwargs)
ax.axis(limits)

Related

Matplotlib clip or trim lines and polygon

How can I efficiently "trim" or "clip" or remove the portion of the red line outside of the purple box? Is there a trick with numpy masks?
Using Python 3.8.3 and Matplotlib
x = [10,15.5,12.5,7.5,5,10]
y = [15,10,5,5,10,15]
fig, ax = plt.subplots()
ax.fill_between(x,y, facecolor="blue", alpha=0.25)
ax.axis("equal")
myinterval = 1.5
xvals = np.arange(min(x), max(x)+1, myinterval)
for i in xvals:
ax.plot([i,i], [0,20], color='red')
This is done not using numpy masks. If I understand correctly, this is the code:
fig, ax = plt.subplots()
x = [10,15.5,12.5,7.5,5,10]
y = [15,10,5,5,10,15]
ax.fill_between(x,y, facecolor="blue", alpha=0.25)
ax.axis("equal")
myinterval = 1.5
xvals = np.arange(min(x), max(x)+1, myinterval)
def generate_equation(x, y):
# y = mx + b
# b = y - mx
left = []
right = []
M = []
B = []
for i in range(len(x)-1):
m = ((y[i+1] - y[i]) / (x[i+1] - x[i]))
b = y[i+1] - m*x[i+1]
M.append(m)
B.append(b)
left.append(min(x[i], x[i+1]))
right.append(max(x[i], x[i+1]))
return M, B, left, right
M, B, left, right = generate_equation(np.array(x), np.array(y))
for i in range(len(xvals)):
ylim = []
for j in range(len(M)):
if xvals[i] >= left[j] and xvals[i] <= right[j]:
Y = M[j] * xvals[i] + B[j]
ylim.append(Y)
ax.vlines(xvals[i], min(ylim), max(ylim), 'r')
Output:

viewing 2D DNA walk with different colours

I am interested in creating a form of RandomWalk, using DNA sequence to create the walk (eg T = up, A = down etc). I have created the code, however i am wanting to know if it is possible for each of the 4 base letters to be assigned a colour instead of the final plot graph only being in one colour?
import matplotlib.pyplot as plt
x = y = 0
x_values = [0]
y_values = [0]
dna_seq = ('GGACTTCCCTATGGTGCTAACAAAGAGGCAGACAAA')
for base in dna_seq:
if base == 'T':
y += 1
elif base == 'A':
y -= 1
elif base == 'G':
x += 1
elif base == 'C':
x -= 1
x_values.append(x)
y_values.append(y)
fig, ax = plt.subplots()
ax.plot(x_values, y_values, c='g')
plt.show()
You can use a dictionary to create a list of colors.
Then, use plt.plot to plot the lines, and plt.scatter for coloured dots:
Adapted version of your code:
import matplotlib.pyplot as plt
x = y = 0
x_values = [0]
y_values = [0]
color_lookup = {'A': 'red',
'T':'green',
'G': 'blue',
'C': 'orange'}
dna_seq = ('GGACTTCCCTATGGTGCTAACAAAGAGGCAGACAAA')
colors = ['k'] # initialise starting point with black
for base in dna_seq:
if base == 'T':
y += 1
elif base == 'A':
y -= 1
elif base == 'G':
x += 1
elif base == 'C':
x -= 1
x_values.append(x)
y_values.append(y)
colors.append(color_lookup[base])
fig, ax = plt.subplots()
ax.plot(x_values, y_values, c='k')
ax.scatter(x_values, y_values, c=colors)
plt.show()
A multicolored line based on this example can be used. The idea is to split the line into sequences and then plot the lines using a LineCollection. Each line of the collection can have is own color.
As the random walker uses a few segments more than once, some of the segments have to be shifted a bit.
import matplotlib.pyplot as plt
x = y = 0.
x_values = [0.]
y_values = [0.]
colors = []
dna_seq = ('GGACTTCCCTATGGTGCTAACAAAGAGGCAGACAAA')#
color_lookup = {'A': 'red',
'T':'green',
'G': 'blue',
'C': 'orange'}
for base in dna_seq:
if base == 'T':
y += 1
elif base == 'A':
y -= 1
elif base == 'G':
x += 1
elif base == 'C':
x -= 1
x_values.append(x)
y_values.append(y)
colors.append(color_lookup[base])
import numpy as np
from matplotlib.collections import LineCollection
points = np.array([x_values, y_values]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
#handle collisions (algorithm could probably be improved :-) )
delta = 0.1
changed=True
while changed:
changed=False
for idx, segment in enumerate(segments):
if idx > 0:
cur_seg = segment.tolist()
if cur_seg in segments[:idx-1].tolist() or [cur_seg[1], cur_seg[0]] in segments[:idx].tolist():
if(cur_seg[0][0] == cur_seg[1][0]):
segment[0][0] += delta
segment[1][0] += delta
else:
segment[0][1] += delta
segment[1][1] += delta
changed=True
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=colors)
lc.set_linewidth(2)
ax.add_collection(lc)
ax.set_aspect('equal')
ax.set_xlim(min(x_values)-.1, max(x_values)+.1)
ax.set_ylim(min(y_values)-.1, max(y_values)+.1)
plt.show()

I can't get same point with homography matrix reverse transform

I get invert of homography matrix
self.inv_homography = np.linalg.inv(self.homography)
and my trasnform function
def doTransform(x, y, homography):
p = np.ndarray(shape=(3, 1), dtype=float, order='F')
p[0, 0] = x
p[1, 0] = y
p[2, 0] = 1
res = np.dot(homography, p)
return res
but third row is not same with first row, there is some pixel slip
ref coords :(768, 512, 1024, 768)
ref to wa coords: 569.5178327464915 185.9395922739289 790.8947327112375 448.7356913249636
wa to ref coords: 767.149391928569 510.19931575332294 1022.283053230326 764.3653307505839
how do I fix this slip ?
I think that you have hardcoded the z coordinate might be the problem. If the z coordinate does not transform to exactly 1, you will introduce an error. This code returns the expected output:
import numpy as np
def transform(x, y, z, homography):
p = np.array([x,y,z]).reshape(3,1)
return np.dot(homography, p)
hom = np.array([1.2,3.1, 4.0, 2.4, 5.4, 3.2, 1.1, 3.0, 1.2]).reshape(3,3)
x, y, z = 2.3, 1.7, 1
inv_hom = np.linalg.inv(hom)
x_wa = transform(x, y, z, hom)[0, 0]
y_wa = transform(x, y, z, hom)[1, 0]
z_wa = transform(x, y, z, hom)[2, 0]
print(transform(x_wa, y_wa, z_wa, inv_hom))
>>[[2.3]
[1.7]
[1. ]]

How to override mpl_toolkits.mplot3d.Axes3D.draw() method?

I'm doing a small project which requires to resolve a bug in matplotlib in order to fix zorders of some ax.patches and ax.collections. More exactly, ax.patches are symbols rotatable in space and ax.collections are sides of ax.voxels (so text must be placed on them). I know so far, that a bug is hidden in draw method of mpl_toolkits.mplot3d.Axes3D: zorder are recalculated each time I move my diagram in an undesired way. So I decided to change definition of draw method in these lines:
for i, col in enumerate(
sorted(self.collections,
key=lambda col: col.do_3d_projection(renderer),
reverse=True)):
#col.zorder = zorder_offset + i #comment this line
col.zorder = col.stable_zorder + i #add this extra line
for i, patch in enumerate(
sorted(self.patches,
key=lambda patch: patch.do_3d_projection(renderer),
reverse=True)):
#patch.zorder = zorder_offset + i #comment this line
patch.zorder = patch.stable_zorder + i #add this extra line
It's assumed that every object of ax.collection and ax.patch has a stable_attribute which is assigned manually in my project. So every time I run my project, I must be sure that mpl_toolkits.mplot3d.Axes3D.draw method is changed manually (outside my project). How to avoid this change and override this method in any way inside my project?
This is MWE of my project:
import matplotlib.pyplot as plt
import numpy as np
#from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.text import TextPath
from matplotlib.transforms import Affine2D
from matplotlib.patches import PathPatch
class VisualArray:
def __init__(self, arr, fig=None, ax=None):
if len(arr.shape) == 1:
arr = arr[None,None,:]
elif len(arr.shape) == 2:
arr = arr[None,:,:]
elif len(arr.shape) > 3:
raise NotImplementedError('More than 3 dimensions is not supported')
self.arr = arr
if fig is None:
self.fig = plt.figure()
else:
self.fig = fig
if ax is None:
self.ax = self.fig.gca(projection='3d')
else:
self.ax = ax
self.ax.azim, self.ax.elev = -120, 30
self.colors = None
def text3d(self, xyz, s, zdir="z", zorder=1, size=None, angle=0, usetex=False, **kwargs):
d = {'-x': np.array([[-1.0, 0.0, 0], [0.0, 1.0, 0.0], [0, 0.0, -1]]),
'-y': np.array([[0.0, 1.0, 0], [-1.0, 0.0, 0.0], [0, 0.0, 1]]),
'-z': np.array([[1.0, 0.0, 0], [0.0, -1.0, 0.0], [0, 0.0, -1]])}
x, y, z = xyz
if "y" in zdir:
x, y, z = x, z, y
elif "x" in zdir:
x, y, z = y, z, x
elif "z" in zdir:
x, y, z = x, y, z
text_path = TextPath((-0.5, -0.5), s, size=size, usetex=usetex)
aff = Affine2D()
trans = aff.rotate(angle)
# apply additional rotation of text_paths if side is dark
if '-' in zdir:
trans._mtx = np.dot(d[zdir], trans._mtx)
trans = trans.translate(x, y)
p = PathPatch(trans.transform_path(text_path), **kwargs)
self.ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=z, zdir=zdir)
p.stable_zorder = zorder
return p
def on_rotation(self, event):
vrot_idx = [self.ax.elev > 0, True].index(True)
v_zorders = 10000 * np.array([(1, -1), (-1, 1)])[vrot_idx]
for side, zorder in zip((self.side1, self.side4), v_zorders):
for patch in side:
patch.stable_zorder = zorder
hrot_idx = [self.ax.azim < -90, self.ax.azim < 0, self.ax.azim < 90, True].index(True)
h_zorders = 10000 * np.array([(1, 1, -1, -1), (-1, 1, 1, -1),
(-1, -1, 1, 1), (1, -1, -1, 1)])[hrot_idx]
sides = (self.side3, self.side2, self.side6, self.side5)
for side, zorder in zip(sides, h_zorders):
for patch in side:
patch.stable_zorder = zorder
def voxelize(self):
shape = self.arr.shape[::-1]
x, y, z = np.indices(shape)
arr = (x < shape[0]) & (y < shape[1]) & (z < shape[2])
self.ax.voxels(arr, facecolors=self.colors, edgecolor='k')
for col in self.ax.collections:
col.stable_zorder = col.zorder
def labelize(self):
self.fig.canvas.mpl_connect('motion_notify_event', self.on_rotation)
s = self.arr.shape
self.side1, self.side2, self.side3, self.side4, self.side5, self.side6 = [], [], [], [], [], []
# labelling surfaces of side1 and side4
surf = np.indices((s[2], s[1])).T[::-1].reshape(-1, 2) + 0.5
surf_pos1 = np.insert(surf, 2, self.arr.shape[0], axis=1)
surf_pos2 = np.insert(surf, 2, 0, axis=1)
labels1 = (self.arr[0]).flatten()
labels2 = (self.arr[-1]).flatten()
for xyz, label in zip(surf_pos1, [f'${n}$' for n in labels1]):
t = self.text3d(xyz, label, zdir="z", zorder=10000, size=1, usetex=True, ec="none", fc="k")
self.side1.append(t)
for xyz, label in zip(surf_pos2, [f'${n}$' for n in labels2]):
t = self.text3d(xyz, label, zdir="-z", zorder=-10000, size=1, usetex=True, ec="none", fc="k")
self.side4.append(t)
# labelling surfaces of side2 and side5
surf = np.indices((s[2], s[0])).T[::-1].reshape(-1, 2) + 0.5
surf_pos1 = np.insert(surf, 1, 0, axis=1)
surf = np.indices((s[0], s[2])).T[::-1].reshape(-1, 2) + 0.5
surf_pos2 = np.insert(surf, 1, self.arr.shape[1], axis=1)
labels1 = (self.arr[:, -1]).flatten()
labels2 = (self.arr[::-1, 0].T[::-1]).flatten()
for xyz, label in zip(surf_pos1, [f'${n}$' for n in labels1]):
t = self.text3d(xyz, label, zdir="y", zorder=10000, size=1, usetex=True, ec="none", fc="k")
self.side2.append(t)
for xyz, label in zip(surf_pos2, [f'${n}$' for n in labels2]):
t = self.text3d(xyz, label, zdir="-y", zorder=-10000, size=1, usetex=True, ec="none", fc="k")
self.side5.append(t)
# labelling surfaces of side3 and side6
surf = np.indices((s[1], s[0])).T[::-1].reshape(-1, 2) + 0.5
surf_pos1 = np.insert(surf, 0, self.arr.shape[2], axis=1)
surf_pos2 = np.insert(surf, 0, 0, axis=1)
labels1 = (self.arr[:, ::-1, -1]).flatten()
labels2 = (self.arr[:, ::-1, 0]).flatten()
for xyz, label in zip(surf_pos1, [f'${n}$' for n in labels1]):
t = self.text3d(xyz, label, zdir="x", zorder=-10000, size=1, usetex=True, ec="none", fc="k")
self.side6.append(t)
for xyz, label in zip(surf_pos2, [f'${n}$' for n in labels2]):
t = self.text3d(xyz, label, zdir="-x", zorder=10000, size=1, usetex=True, ec="none", fc="k")
self.side3.append(t)
def vizualize(self):
self.voxelize()
self.labelize()
plt.axis('off')
arr = np.arange(60).reshape((2,6,5))
va = VisualArray(arr)
va.vizualize()
plt.show()
This is an output I get after external change of ...\mpl_toolkits\mplot3d\axes3d.py file:
This is an output (an unwanted one) I get if no change is done:
What you want to achieve is called Monkey Patching.
It has its downsides and has to be used with some care (there is plenty of information available under this keyword). But one option could look something like this:
from matplotlib import artist
from mpl_toolkits.mplot3d import Axes3D
# Create a new draw function
#artist.allow_rasterization
def draw(self, renderer):
# Your version
# ...
# Add Axes3D explicitly to super() calls
super(Axes3D, self).draw(renderer)
# Overwrite the old draw function
Axes3D.draw = draw
# The rest of your code
# ...
Caveats here are to import artist for the decorator and the explicit call super(Axes3D, self).method() instead of just using super().method().
Depending on your use case and to stay compatible with the rest of your code you could also save the original draw function and use the custom only temporarily:
def draw_custom():
...
draw_org = Axes3D.draw
Axes3D.draw = draw_custom
# Do custom stuff
Axes3D.draw = draw_org
# Do normal stuff

Include matplotlib in pyqt5 with hover labels

I have a plot from matplotlib for which I would like to display labels on the marker points when hover over with the mouse.
I found this very helpful working example on SO and I was trying to integrate the exact same plot into a pyqt5 application.
Unfortunately when having the plot in the application the hovering doesn't work anymore.
Here is a full working example based on the mentioned SO post:
import matplotlib.pyplot as plt
import scipy.spatial as spatial
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import sys
pi = np.pi
cos = np.cos
def fmt(x, y):
return 'x: {x:0.2f}\ny: {y:0.2f}'.format(x=x, y=y)
class FollowDotCursor(object):
"""Display the x,y location of the nearest data point.
https://stackoverflow.com/a/4674445/190597 (Joe Kington)
https://stackoverflow.com/a/13306887/190597 (unutbu)
https://stackoverflow.com/a/15454427/190597 (unutbu)
"""
def __init__(self, ax, x, y, tolerance=5, formatter=fmt, offsets=(-20, 20)):
try:
x = np.asarray(x, dtype='float')
except (TypeError, ValueError):
x = np.asarray(mdates.date2num(x), dtype='float')
y = np.asarray(y, dtype='float')
mask = ~(np.isnan(x) | np.isnan(y))
x = x[mask]
y = y[mask]
self._points = np.column_stack((x, y))
self.offsets = offsets
y = y[np.abs(y-y.mean()) <= 3*y.std()]
self.scale = x.ptp()
self.scale = y.ptp() / self.scale if self.scale else 1
self.tree = spatial.cKDTree(self.scaled(self._points))
self.formatter = formatter
self.tolerance = tolerance
self.ax = ax
self.fig = ax.figure
self.ax.xaxis.set_label_position('top')
self.dot = ax.scatter(
[x.min()], [y.min()], s=130, color='green', alpha=0.7)
self.annotation = self.setup_annotation()
plt.connect('motion_notify_event', self)
def scaled(self, points):
points = np.asarray(points)
return points * (self.scale, 1)
def __call__(self, event):
ax = self.ax
# event.inaxes is always the current axis. If you use twinx, ax could be
# a different axis.
if event.inaxes == ax:
x, y = event.xdata, event.ydata
elif event.inaxes is None:
return
else:
inv = ax.transData.inverted()
x, y = inv.transform([(event.x, event.y)]).ravel()
annotation = self.annotation
x, y = self.snap(x, y)
annotation.xy = x, y
annotation.set_text(self.formatter(x, y))
self.dot.set_offsets((x, y))
bbox = ax.viewLim
event.canvas.draw()
def setup_annotation(self):
"""Draw and hide the annotation box."""
annotation = self.ax.annotate(
'', xy=(0, 0), ha = 'right',
xytext = self.offsets, textcoords = 'offset points', va = 'bottom',
bbox = dict(
boxstyle='round,pad=0.5', fc='yellow', alpha=0.75),
arrowprops = dict(
arrowstyle='->', connectionstyle='arc3,rad=0'))
return annotation
def snap(self, x, y):
"""Return the value in self.tree closest to x, y."""
dist, idx = self.tree.query(self.scaled((x, y)), k=1, p=1)
try:
return self._points[idx]
except IndexError:
# IndexError: index out of bounds
return self._points[0]
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.width = 1000
self.height = 800
self.setGeometry(0, 0, self.width, self.height)
canvas = self.get_canvas()
w = QWidget()
w.layout = QHBoxLayout()
w.layout.addWidget(canvas)
w.setLayout(w.layout)
self.setCentralWidget(w)
self.show()
def get_canvas(self):
fig, ax = plt.subplots()
x = np.linspace(0.1, 2*pi, 10)
y = cos(x)
markerline, stemlines, baseline = ax.stem(x, y, '-.')
plt.setp(markerline, 'markerfacecolor', 'b')
plt.setp(baseline, 'color','r', 'linewidth', 2)
cursor = FollowDotCursor(ax, x, y, tolerance=20)
canvas = FigureCanvas(fig)
return canvas
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
What would I have to do to make the labels also show when hovering over in the pyqt application?
The first problem may be that you don't keep a reference to the FollowDotCursor.
So to make sure the FollowDotCursor stays alive, you can make it a class variable
self.cursor = FollowDotCursor(ax, x, y, tolerance=20)
instead of cursor = ....
Next make sure you instatiate the Cursor class after giving the figure a canvas.
canvas = FigureCanvas(fig)
self.cursor = FollowDotCursor(ax, x, y, tolerance=20)
Finally, keep a reference to the callback inside the FollowDotCursor and don't use plt.connect but the canvas itself:
self.cid = self.fig.canvas.mpl_connect('motion_notify_event', self)