Plotnine: Python Alternative to ggplot2


Using Python floating libraries like matplotlib and seaborn can also produce exquisite graphics, but is difficult to implement because compared to ggplot2's simple, readable, and layered approach in R, there is no standardized syntax for implementing the graphic syntax.

Experienced users know that ggplot2 can make life easier when it comes to explorative data analysis and data visualization. You can easily create elegant, powerful charts that help you decipher the underlying relationships in your data.

Using Python floating libraries like matplotlib and seaborn can also produce exquisite graphics, but is difficult to implement because compared to ggplot2's simple, readable, and layered approach in R, there is no standardized syntax for implementing the graphic syntax.

The answer to this problem lies in Plotnine.

The style I want to say is similar to Rs ggplot2, which is 99%. The main difference is the use of parentheses, as shown in some brief examples below. One of the best ways to use Pilantine is that the output is essentially the same as in R. No obvious difference.

There are many API options in the drawing that can be used to create the drawing.
(
ggplot (mtcars, aes ('wt', 'mpg', color = 'factor (cyl)')
+ geom_point ()
+ Laboratory (title = "micron / mile vs weight", x = "weight", y = "micron / mile")
+ Guide (color = guide_legend (title = 'number of cylinders))
One of the main selling points of ggplot in R is the FACET function. There are many options for drawing a subset of data on a single line of code.

(Ggplot (mtcars, aes ('wt', 'mpg', color = 'factor (cyl)')
+ geom_point ()
+ Laboratory (title = "micron / mile vs weight", x = "weight", y = "micron / mile")
+ Guide (color = guide_legend (title = "cylinder"))
+ facet_wrap ("~ gear")
)
Adding facet_wrap ("~ gear") to the end of the previous code generates a facet. This is actually easier than using Matplotlib and Seaborn. Matplotlib must create a separate diagram for each set of variables to be displayed (for example, the diagram above contains 3 diagrams, so you have to create 3 diagrams). Seaborn is easier than Matplotlib, but you have to use something else. Commands that can confuse inexperienced users.

Aesthetic Improvements


If these visual effects are not formatted correctly, you do not need to create all the visual effects.
(Ggplot (mtcars, aes ('wt', 'mpg', color = 'factor (cyl)', size = 'hp'))
+ geom_point ()
+ theme_bw ()
+ Laboratory (title = "micron / mile vs weight", x = "weight", y = "micron / mile")
+ Guide (color = guide_legend (title = "cylinder"))
+ facet_wrap ("~ gear")
)
Adding size = 'hp' gives you further insight into your data (the size of your horsepower) and theme_bw () to provide a beautifully simple theme to draw in a standardized format. theme_bw () is a theme command that every R user from ggplot2 knows. Basically, it's a de facto topic before trying different topics and formats.

Learn how to integrate ipywidget into Plotnine, Jupyter Notebook and JupyterLab.

If you go deeper, you'll find that Plotnine offers a simple API and nice visual effects that you can get with ggplot2 in R. The ability to format graphics with a single line of code is available in Seaborn, but not in Matplotlib. Seaborn itself has some similarities to Plotnine and ggplot2, but the easy-to-understand syntax is a unique selling point when creating switches.