Commit 6590d845 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

set black arrows for PCA correlation circle, alpha is arrow length

parent a3f8a79e
import json
import io
import base64
import itertools
import math
from import BaseCommand
from django.forms.models import model_to_dict
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
......@@ -28,50 +31,6 @@ class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("Generating the PCA biplot..."))
pca_data = []
features = [
features = [
......@@ -90,7 +49,7 @@ class Command(BaseCommand):
] # MISSING: ArRing - Number of Aromatic ring
self.stdout.write("Successfully flushed PCA biplot data"))
values_list = []
......@@ -123,13 +82,16 @@ class Command(BaseCommand):
pc_infos = pd.DataFrame(
coef, columns=cols, index=features
# we might remove the line below if the PCA remains grayscale
pal = itertools.cycle(sns.color_palette("dark", len(features)))
for idx in range(len(pc_infos["PC-0"])):
x = pc_infos["PC-0"][idx]
y = pc_infos["PC-1"][idx]
plt.plot([0.0, x], [0.0, y], "k-")
plt.plot(x, y, 'h', color='cadetblue')
plt.annotate(pc_infos.index[idx], xy=(x,y), xycoords='data',
color = "black"
alpha = math.sqrt(abs(x)*abs(x)+abs(y)*abs(y))
plt.arrow(0.0, 0.0, x, y, head_width=0.02, color="black", alpha=alpha)
plt.annotate(Compound._meta.get_field(pc_infos.index[idx]).verbose_name, xy=(x,y), xycoords='data',
xytext=np.asarray((x,y))+(0.02,-0.02), fontsize=6, color=color, alpha=alpha)
plt.xlabel("PC-0 (%s%%)" % str(variance_ratio[0])[:4].lstrip("0."))
plt.ylabel("PC-1 (%s%%)" % str(variance_ratio[1])[:4].lstrip("0."))
plt.xlim((-1, 1))
......@@ -18,6 +18,7 @@ bs4
scikit-learn==0.20.3 #later versions do not support Python<3.5
scipy==1.2.1 #Scipy >=1.3 does not support Python<3.5
matplotlib==2.2.3 #Matplotlib 3.0+ does not support Python 2.x, 3.0, 3.1, 3.2, 3.3, or 3.4.
# postgres driver
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment