R Lattice like graphs with Python, Pandas and Matplotlib

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

+5
source share
1 answer

Seaborn is the most efficient library I've found for creating faceted graphs in python. Its pandas wrapper around matplotlib, which takes care of all the sub-tasks for you, and updates the matplotlib style to look more modern. It produces some really excellent results.

The fragment is performed using the grid part of the library.

This is slightly different from R in that you first create a grid and pass the data into it, as well as the faces you want, rows, columns, colors, etc. Then you map the plotting functions to this grid, passing any necessary arguments to the displayed plotting functions.

 #scatter plot one factor import seaborn as sns grid1 = sns.FacetGrid(df, col='c1') grid1.map(plt.scatter, 'x1', 'x2') #scatter plot with column and hue factor grid2 = sns.FacetGrid(df, col='c1', hue='c2') grid2.map(plt.scatter, 'x1', 'x2') #histogram with one factor grid3 = sns.FacetGrid(df, col='c1') grid3.map(plt.hist, 'x1', alpha=.7) 
+8
source

Source: https://habr.com/ru/post/1202521/


All Articles