Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ggplot-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Using ggplot in Python: Visualizing Data With plotnine

This folder provides the code examples for the Real Python tutorial [Using ggplot in Python: Visualizing Data With plotnine](https://realpython.com/ggplot-python/).

## Setup

Create and activate a virtual environment, then install the dependencies:

```console
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install -r requirements.txt
```

The code requires Python 3.10 or later.

## Usage

Each script matches a section of the tutorial. Run any of them to display its plots:

```console
(venv) $ python first_plot.py
```

The tutorial runs the examples in Jupyter Notebook, where each plot shows up inline. You can paste the code from any script into a notebook cell to get the same result.

Running `export.py` saves the plot to `myplot.png` in the current folder.
7 changes: 7 additions & 0 deletions ggplot-python/coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Section: Coordinates Systems: Map Data Values to 2D Space

from plotnine import aes, coord_flip, geom_bar, ggplot
from plotnine.data import mpg

(ggplot(mpg) + aes(x="class") + geom_bar()).show()
(ggplot(mpg) + aes(x="class") + geom_bar() + coord_flip()).show()
15 changes: 15 additions & 0 deletions ggplot-python/data_aesthetics_geoms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Section: Plotting Data Using Python and ggplot

from plotnine import aes, geom_bar, geom_point, ggplot
from plotnine.data import mpg

# Data: The Source of Information
print(mpg)
ggplot(mpg).show()

# Aesthetics: Define Variables for Each Axis
(ggplot(mpg) + aes(x="class", y="hwy")).show()

# Geometric Objects: Choose Different Plot Types
(ggplot(mpg) + aes(x="class", y="hwy") + geom_point()).show()
(ggplot(mpg) + aes(x="class") + geom_bar()).show()
7 changes: 7 additions & 0 deletions ggplot-python/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Section: Exporting Plots to Files

from plotnine import aes, geom_line, ggplot
from plotnine.data import economics

my_plot = ggplot(economics) + aes(x="date", y="pop") + geom_line()
my_plot.save("myplot.png", dpi=600)
16 changes: 16 additions & 0 deletions ggplot-python/facets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Section: Facets: Plot Subsets of Data Into Panels in the Same Plot

from plotnine import aes, facet_grid, geom_point, ggplot, labs
from plotnine.data import mpg

(
ggplot(mpg)
+ facet_grid(rows="year", cols="class")
+ aes(x="displ", y="hwy")
+ labs(
x="Engine Size",
y="Miles per Gallon",
title="Miles per Gallon for Each Year and Vehicle Class",
)
+ geom_point()
).show()
12 changes: 12 additions & 0 deletions ggplot-python/first_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Section: Building Your First Plot With ggplot and Python

from plotnine import aes, geom_line, ggplot
from plotnine.data import economics

print(economics)

(
ggplot(economics) # What data to use
+ aes(x="date", y="pop") # What variable to use
+ geom_line() # Geometric object to use for drawing
).show()
16 changes: 16 additions & 0 deletions ggplot-python/multidimensional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Section: Visualizing Multidimensional Data

from plotnine import aes, geom_point, ggplot, labs
from plotnine.data import mpg

(
ggplot(mpg)
+ aes(x="cyl", y="hwy", color="class")
+ labs(
x="Engine Cylinders",
y="Miles per Gallon",
color="Vehicle Class",
title="Miles per Gallon for Engine Cylinders and Vehicle Classes",
)
+ geom_point()
).show()
2 changes: 2 additions & 0 deletions ggplot-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plotnine==0.15.9
jupyter==1.1.1
12 changes: 12 additions & 0 deletions ggplot-python/scales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Section: Scales: Change Data Scale According to Its Meaning

from plotnine import aes, geom_line, ggplot, labs, scale_x_timedelta
from plotnine.data import economics

(
ggplot(economics)
+ aes(x="date - date.min()", y="pop")
+ scale_x_timedelta(name="Time since July 1967")
+ labs(title="Population Evolution", y="Population")
+ geom_line()
).show()
18 changes: 18 additions & 0 deletions ggplot-python/statistical_transformations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Section: Statistical Transformations: Aggregate and Transform Your Data

from plotnine import (
aes,
geom_bar,
geom_boxplot,
geom_histogram,
ggplot,
stat_bin,
)
from plotnine.data import huron

# Levels of Lake Huron 1875-1975
print(huron)

(ggplot(huron) + aes(x="level") + stat_bin(bins=10) + geom_bar()).show()
(ggplot(huron) + aes(x="level") + geom_histogram(bins=10)).show()
(ggplot(huron) + aes(x="factor(decade)", y="level") + geom_boxplot()).show()
17 changes: 17 additions & 0 deletions ggplot-python/themes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Section: Themes: Improve the Look of Your Visualization

from plotnine import aes, facet_grid, geom_point, ggplot, labs, theme_dark
from plotnine.data import mpg

(
ggplot(mpg)
+ facet_grid(rows="year", cols="class")
+ aes(x="displ", y="hwy")
+ labs(
x="Engine Size",
y="Miles per Gallon",
title="Miles per Gallon for Each Year and Vehicle Class",
)
+ geom_point()
+ theme_dark()
).show()
Loading