diff --git a/ggplot-python/README.md b/ggplot-python/README.md new file mode 100644 index 0000000000..1daca71f13 --- /dev/null +++ b/ggplot-python/README.md @@ -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. diff --git a/ggplot-python/coordinates.py b/ggplot-python/coordinates.py new file mode 100644 index 0000000000..f098db2ed3 --- /dev/null +++ b/ggplot-python/coordinates.py @@ -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() diff --git a/ggplot-python/data_aesthetics_geoms.py b/ggplot-python/data_aesthetics_geoms.py new file mode 100644 index 0000000000..e11bfcd06d --- /dev/null +++ b/ggplot-python/data_aesthetics_geoms.py @@ -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() diff --git a/ggplot-python/export.py b/ggplot-python/export.py new file mode 100644 index 0000000000..dff48e8c91 --- /dev/null +++ b/ggplot-python/export.py @@ -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) diff --git a/ggplot-python/facets.py b/ggplot-python/facets.py new file mode 100644 index 0000000000..2c145878df --- /dev/null +++ b/ggplot-python/facets.py @@ -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() diff --git a/ggplot-python/first_plot.py b/ggplot-python/first_plot.py new file mode 100644 index 0000000000..f28a7a584c --- /dev/null +++ b/ggplot-python/first_plot.py @@ -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() diff --git a/ggplot-python/multidimensional.py b/ggplot-python/multidimensional.py new file mode 100644 index 0000000000..b5f787c770 --- /dev/null +++ b/ggplot-python/multidimensional.py @@ -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() diff --git a/ggplot-python/requirements.txt b/ggplot-python/requirements.txt new file mode 100644 index 0000000000..ea063d2eae --- /dev/null +++ b/ggplot-python/requirements.txt @@ -0,0 +1,2 @@ +plotnine==0.15.9 +jupyter==1.1.1 diff --git a/ggplot-python/scales.py b/ggplot-python/scales.py new file mode 100644 index 0000000000..be0cf47180 --- /dev/null +++ b/ggplot-python/scales.py @@ -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() diff --git a/ggplot-python/statistical_transformations.py b/ggplot-python/statistical_transformations.py new file mode 100644 index 0000000000..50e1a2c73e --- /dev/null +++ b/ggplot-python/statistical_transformations.py @@ -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() diff --git a/ggplot-python/themes.py b/ggplot-python/themes.py new file mode 100644 index 0000000000..415fe6acf6 --- /dev/null +++ b/ggplot-python/themes.py @@ -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()