I have a pandas data frame of "factors", floats and integers. I would like to make "R Lattice" look like graphs using conditionality and grouping by categorical variables. I made extensive use of R and wrote custom panel functions to get graphs formatted exactly the way I wanted them, but I am struggling with matplotlib to make the same types of plots concisely. I play with layouts and subplot2grid but just can't figure out how to do it right.
import numpy as np import pandas as pd import matplotlib.pyplot as plt nRows = 500 df = pd.DataFrame({'c1' : np.random.choice(['A','B','C','D'], size=nRows), 'c2' : np.random.choice(['P','Q','R'], size=nRows), 'i1' : np.random.randint(20,50, nRows), 'i2' : np.random.randint(0,10, nRows), 'x1' : 3 * np.random.randn(nRows) + 90, 'x2' : 2 * np.random.randn(nRows) + 89})
I would like to build things like the following (R lattice code examples)
x1 vs x2 for each level c1 (trellis code)
xyplot(x1 ~ x2 | c1, data = df)
x1 vs x2 for each level c1 with the "global" legend c2 (characters or colors)
xyplot(x1 ~ x2 | c1, groups = c2, data = df)
histograms x1 for each c2
hist (~x1 | c1, data = df)
I also try to make "conditional" contour graphs, such as those created here (1.4.4.4)
https://scipy-lectures.imtqy.com/intro/matplotlib/matplotlib.html
I read these examples: http://nbviewer.ipython.org/github/fonnesbeck/Bios366/blob/master/notebooks/Section2_4-Matplotlib.ipynb
However, I would like the layout to be generated from among the levels in the categorical conditional (or "by") variable (s). that is, indicate the number of columns, and the rows will be calculated based on the quantity levels.
Evaluate any helpful tips or steps in the right direction. I would prefer not to use rpy2 or python ggplot (I messed with them), and found them disappointing and limiting). A.
Thanks! Randall