Hi Statalisters,

With the release of Stata16 and the fantastic integration of Python. I have decided to walk myself through in the Python world. I wanted to replicate a Stata code for graphing individual scatter matrix through for loops in Python, as Python gives me the opportunity of including kernel density.

The Stata syntax I need to replicate:
Code:
*create data
clear
set seed 25656894
set obs 200
gen weight = (runiform()+1) *50
gen foreign = runiformint(1, 3)
sort foreign
keep weight foreign 
expand 3
egen id2 = cut(weight), group(3)
sort id2
bysort id2: gen id = _n
drop id2 foreign
sort id
bysort id: gen grp = _n
reshape wide weight, i(id) j(grp)
replace weight3= (runiform()+1) *50 if weight3==.
egen rec = cut(weight1), group(4)

*graph scatter matrix
forv i=0/3 {
foreach w in weight  {
    graph matrix `w'1 `w'2 `w'3 if rec==`i' ,  /// 
        name(`w'`i', replace) xsize(4) scheme(s1color)
    }
}
save "py_example.dta", replace
The individual image is attached here:

Now if I call the above data to plot with Stata, the graph does not plot individual for each rec as I indicated above (if rec==`i'). Rather it colors based on categories of rec. I need help with adjusting my Python as pect of the code to replicate what I have specified in my Stata code above:

Code:
*python
python clear
python:
import pandas as pd
import seaborn as sns, numpy as np
import matplotlib.pyplot as plt
data = pd.read_stata('py_example.dta')
print(data)
sns.pairplot(data, kind="scatter", diag_kind="kde", vars=["weight1", "weight2", "weight3"], hue="rec")
plt.show()
end
The graph from python is also attached.

Thanks you