• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / ggplot2 in python / Plotnine: A Python library to use ggplot2 in Python

Plotnine: A Python library to use ggplot2 in Python

May 2, 2018 by cmdlinetips

Plotnine Python Library
Plotnine Python Library

Python’s visualization options can be difficult to grasp, if you recently moved to use Python after using R for a while. There are a lot of options. Multiple options for visualizations are good. Especially, mature matplotlib, Seaborn, Bokeh and the new library like Altair are great to learn and use for data visualization in Python.

In last year’s PyCon, Jake gave a fantastic talk capturing the landscape of Python Visualization libraries. Yes, it is complex 🙂 The talk is a must watch if you are interested in data visualization in Python.

Python Visualization Landscape
Python Visualization Landscape

ggplot2 library is one of the best data visualization libraries. If you are coming from R background  and know ggplot2, you might want to still use ggplot2 in Python for making great visualizations. Thanks to plotnine library, you can use ggplot2 right from Python.

Let us load some data to make plots with plotnine.

 
gapminder_url='https://bit.ly/2cLzoxH'
gapminder = pd.read_csv(gapminder_url)
gapminder.head()

To make a scatterplot with plotnine in Python, we just simply need to type the ggplot2 commands

 
(ggplot(gapminder)
 + geom_point(aes(x='gdpPercap', y='lifeExp'), alpha=0.5, size=2)
 + ggtitle('plotnine example: scatter plot')
 + scale_x_log10()
)
scatter_plot_with_plotnine1
scatter_plot_with_plotnine1

Let us consider another example of making scatter plot and saving to a file. We can save ggplot object as a variable and use ggsave to save the plot into a file.

 
p = ggplot(gapminder) + 
    geom_point(aes(x='gdpPercap', y='lifeExp',color='continent')) + 
    ggtitle('plotnine example: scatter plot')
ggsave(plot=p, filename='scatter_plot_with_plotnine.png', dpi=100)

Scatter plot with plotnine in Python using ggplot
Scatter plot with plotnine in Python using ggplot

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

How To Highlight Select Data Points with ggplot2 in R? ggplot2 change legend title with guides()How To Change Legend Title in ggplot2? Adjusting Transparency in Scatter PlotHow To Make Scatter Plot in Python with Seaborn? Altair: A Python Visualization LibraryAn Introduction to Altair: A Python Visualization Library

Filed Under: ggplot2 in python, Plotnine python library, Python Tagged With: Data Visualization in Python, ggplot2 in Python, plotnine python library

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version