Easily create geofaceted plots in matplotlib
Project description
geofacetpy
geofacetpy is a Python library built to simplify the creation of geofaceted plots using matplotlib. It allows to easily map data to a grid layout and visualize trends across different regions using matplotlib and seaborn.
This library was heavily inspired by the R library geofacet.
Installation
pip install geofacetpy
Usage
Before you start [IMPORTANT]
Grid
Currently, the grid has to be a pandas DataFrame
with specific columns - row
, col
and string column that serves as a label (default name
).
row | col | name |
---|---|---|
6 | 7 | Alabama |
1 | 1 | Alaska |
6 | 2 | Arizona |
6 | 5 | Arkansas |
6 | 1 | California |
There's a large repository of grids, which follow the same data structure at hafen/grid-desginer.
Custom plotting function
The custom plotting function, that is supplied to the geofacet()
must take the following arguments
ax
(Axes
object),data
group_name
(name of the column in data that corresponds to string column with label in grid)
def custom_plot(ax, data, group_name):
ax.bar(data['col_x'], data['col_y'], color="blue")
ax.set_title(group_name, fontsize=8)
ax.tick_params(axis="x", labelsize=8)
ax.grid(True, linestyle="--", linewidth=0.5)
geofacet
To create a geofaceted plot, use geofacet()
.
Supply the following arguments:
grid_layout
: pd.DataFrame with griddata
: pd.DataFrame with datagroup_column
: column name indata
to be used as a facet, basis for placement on the gridgrid_col
: column name ingrid_layout
with label (optional, if different thanname
)plotting_function
: callable, function to draw a plot for each grid element
from geofacetpy import geofacet
fig, axes = geofacet(
grid_layout=grid,
data=data,
group_column="district",
plotting_function=custom_plot,
sharex=True,
sharey=True,
)
Examples
Creating a geofacet plot
from geofacet import geofacet
import pandas as pd
import matplotlib.pyplot as plt
# Load data and grid layout
data = pd.read_csv("data_grouped.csv")
grid = pd.read_csv("grid.csv")
# Define a custom plotting function
def custom_plot(ax, data, group_name):
ax.bar(data['col_x'], data['col_y'], color="blue")
ax.set_title(group_name.replace(" ", "\n"), fontsize=8)
ax.tick_params(axis="x", labelsize=8)
ax.grid(True, linestyle="--", linewidth=0.5)
# Create the geofaceted plot
fig, axes = geofacet(
grid_layout=grid,
data=data,
group_column="district",
plotting_function=custom_plot,
figure_size=(11, 9),
grid_spacing=(0.5, 0.5),
sharex=True,
sharey=True,
)
# Add titles and labels
fig.suptitle("Example Geofaceted Plot")
fig.supxlabel("Year")
fig.supylabel("Count")
plt.show()
Creating a Geofacet Plot with Seaborn
from geofacet import geofacet
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load data and grid layout
data = pd.read_csv("data_grouped.csv")
grid = pd.read_csv("grid.csv")
# Define a custom plotting function using Seaborn
def seaborn_plot(ax, data, group_name):
sns.lineplot(ax=ax, data=data, x='col_x', y='col_y', marker="o")
ax.set_title(group_name, fontsize=8)
ax.tick_params(axis="x", labelsize=8)
ax.grid(True, linestyle="--", linewidth=0.5)
# Create the geofaceted plot
fig, axes = geofacet(
grid_layout=grid,
data=data,
group_column="district",
plotting_function=seaborn_plot,
figure_size=(11, 9),
grid_spacing=(0.5, 0.5),
sharex=True,
sharey=True,
)
# Add titles and labels
fig.suptitle("Geofaceted Plot with Seaborn")
fig.supxlabel("Year")
fig.supylabel("Count")
plt.show()
Output Example
Previewing Grid Layout
If the label column is not name
, pass it in grid_col
argument.
from geofacet import preview_grid
import pandas as pd
grid = pd.read_csv("grid.csv")
preview_grid(grid)
or
grid_2 = pd.read_csv("grid.csv")
preview_grid(grid_2, grid_col="label")
Contributing
Feel free to open an issue for suggestions, report bugs, or submit a pull request to improve the library.
License
This project is licensed under the MIT License. See the LICENSE
file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file geofacetpy-0.1.1.tar.gz
.
File metadata
- Download URL: geofacetpy-0.1.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de691ff07db5a0754240219bb4fa22ba0cb5fd0e8a4873c0fd85ce3a2d47a959 |
|
MD5 | f100f4366afb75fa120b42f44e2a39fb |
|
BLAKE2b-256 | 978878d3c2bce05571773de8bf562d451b502e0bdff0f41f283d03aaf6c79160 |
File details
Details for the file geofacetpy-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: geofacetpy-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aebc8adf2180d5571c5861a996837cb8ce2584904f093fb2ba09f719b2c4a5b |
|
MD5 | 4a2488599163f43122f18866a0ef8f77 |
|
BLAKE2b-256 | fc0ed5740d0a4e53071fb58904087c57f8c1df8f2c34daa3eb2a48848b3db77e |