Plotnine: ggplot2 in Python
By: Asel Mendis
Tags: Data Science, Data Visualization, Python, R, Technology

Plotnine is Python’s answer to ggplot2 in R. R users will feel right at home with this data visualization package with a highly similar syntax with minor syntactic differences.

R has been the ruler of data visualization and statistical modelling between the two while Python was the best for productionizing / monetizing data science. The ggplot2 package from R is a child of Hadley Wickham’s ingenuity. It undoubtedly allows anyone who can code in R to use a declarative approach to creating stunning visuals for their work. There are also a number of extensions that can extends ggplot2 even further.

Python has Matplotlib and Seaborn for its external plotting library. Seaborn is built on top of Matplotlib and is a dependency. Seaborn helps the user accomplish what would be done in Matplotlib with much less code and allows the user to use Matplotlib commands to manipulate the figure as well.

Undoubtedly we are spoiled for choice once we have a good grasp of both languages. However lone Python users have not been able to experience the beauty and simplicity of ggplot2 in its native Python until now (to a certain extent).

Thanks to Hassan Kibirige, the creator of plotnine. Plotnine is the competitor of R in Python. Its syntax is probably 95% similar or more to ggplot2. It has a table release and is still active on Github. It already does a lot of what users love most about ggplot2 such as access to various geoms, declarative syntax and faceting for example. There are many R users that realize the importance of Python in their skillset and vice versa. Plotnine is another way that makes a comfortable transition between the languages. Plotnine is still an infant in comparison to its counterpart ggplot2 but the potential is huge if active development continues.

Plotnine

As Python’s designated ggplot2, we shall now see how the syntax and visuals match up with each other. One does not replace the other but if different jobs can be done on different languages, certain people can get an idea on what language to conduct their data visualization tasks.

A tip about plotnine installation that some users may experience (not all): When installing plotnine make sure you are using the updated version of your package manager (i.e. pip or conda). If you are using Jupyter, for some reason it may not work but it will work on other IDE’s like Spyder. This was the case for me and I haven’t figured out the reason yet.

Below is a standard example of using plotnine. R users will notice that the syntax and output are strikingly similar.

(

ggplot(mtcars, aes(‘wt’, ‘mpg’, color=’factor(cyl)’))

+ geom_point()

+ labs(title=’Miles per gallon vs Weight’, x=’Weight’, y=’Miles per gallon’)

+ guides(color=guide_legend(title=’Number of Cylinders’)) )

There are really only two noticeable differences in the syntax:

ggplot(mtcars, aes(‘wt’, ‘mpg’, color=’factor(cyl)’)) — we can see that ‘factor(cyl)’ is a striking resemblance to the method of converting data into a category in R. There are other ways to convert a numeric column into a category but this seems a nice nod to R users.

The whole syntax is actually enclosed in parenthesis (brackets). This is due to indentation issues in Python. Because Python relies so much on indentation for functions, brackets become necessary to execute the code.

To take it to the next step, faceting the plot is also simple and easy with a minor tweak to make it in plotnine:

(ggplot(mtcars, aes(‘wt’, ‘mpg’, color=’factor(cyl)’))

+ geom_point()

+ labs(title=’Miles per gallon vs Weight’,

x=’Weight’, y=’Miles per gallon’)

+ guides(color=guide_legend(title=’Cylinders’))

+ facet_wrap(‘~gear’)

)

The last line of code has (as R users would know) facet_wrap and the only difference is the argument needs to be in colons.

A scatterplot is ideal for addding many dimensions of data. Now we shall see how to change the sizes of the points to represent horse power:

(ggplot(mtcars, aes(‘wt’, ‘mpg’, color=’factor(cyl)’, size = ‘hp’))

+ geom_point()

+ labs(title=’Miles per gallon vs Weight’,

x=’Weight’, y=’Miles per gallon’)

+ guides(color=guide_legend(title=’Cylinders’))

+ facet_wrap(‘~gear’)

)

Once again just a simple command and a barely noticeable difference. We have added the size=’hp’ argument to the aesthetics so that each point will represent a different level of horsepower. We can see that higher the weight engine, the more horsepower it has.

Any plot needs to look their best. Themes are a great way to add some default formatting in order to get started. Plotnine appears to have some of the same themes from ggplot2 as well:

(ggplot(mtcars, aes(‘wt’, ‘mpg’, color=’factor(cyl)’, size = ‘hp’))

+ geom_point()

+ theme_bw()

+ labs(title=’Miles per gallon vs Weight’,

x=’Weight’, y=’Miles per gallon’)

+ guides(color=guide_legend(title=’Cylinders’))

+ facet_wrap(‘~gear’)

)

Just by adding theme_bw() we can get a rather nicer and more aesthetically pleasing plot.

Conclusion

We can see that plotnine and ggplot2 have much in common. Transitioners to Python from R will definitely feel more at home when conducting data visualization in Python with plotnine.

One of the best things about plotnine is that is still active on Github as of July 2019 and hopefully it is a good sign that it will continue to be updated to become the ggplot2 of Python. I say that because ggplot2 has so many extensions and complementary packages that make it powerful and wanted. Plotnine is undoubtedly doing a great job and I would definitely use it myself.