Make figures with context managers in python: quicker, simpler, more readable.
Project description
Figurex
Make figures with context managers in python: quicker, simpler, more readable.
with Figure() as ax:
ax.plot([1,2],[3,4])
Idea
Tired of lengthy matplotlib code just for simple plotting?
# How plotting used to be:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1,2, figsize=(4,5))
plt.set_title("My plot")
ax = axes[0]
ax.plot([1,2],[3,4])
ax = axes[1]
ax.plot([2,3],[4,5])
fig.savefig("file.png", bbox_inches='tight')
plt.show()
Beautify your daily work with shorter and more readable code:
# How plotting becomes with figurex:
from figurex import Figure, Panel
with Figure("My plot", layout=(1,2), size=(4,5), save="file.png"):
with Panel() as ax:
ax.plot([1,2],[3,4])
with Panel() as ax:
ax.plot([2,3],[4,5])
The Figure()
environment generates the matplotlib
-based figure and axes for you, and automatically shows, saves, and closes the figure when leaving the context. It is just a wrapper around standard matplotlib code, you can use ax
to modify the plot as you would normally do. Extend it your way without limits!
Examples
Make a simple plot:
with Figure("A simple plot") as ax:
ax.plot([1,2],[3,4])
A plot with two panels:
with Figure(layout=(1,2), size=(6,3)):
with Panel("a) Magic") as ax:
ax.plot([1,2],[3,4])
with Panel("b) Reality", grid="") as ax:
ax.plot([5,5],[6,4])
Save a plot into memory for later use (e.g. in FPDF):
with Figure("Tea party", show=False):
with Panel() as ax:
ax.plot([5,5],[6,4])
my_figure = Figure.as_object()
# <_io.BytesIO at 0x...>
Plotting maps:
from figurex import Basemap
with Figure(size=(3,3)):
with Basemap("Germany", extent=(5,15,46,55), tiles="relief") as Map:
x,y = Map(12.385, 51.331)
Map.scatter(x, y, marker="x", color="red", s=200)
- Check out the Examples Notebook!
Install
pip install figurex
Requirements
- Minimal requirements (basic plotting):
- python >3.9
- numpy
- matplotlib
- neatlogger (wrapper for loguru, logging)
- If you want to make geographic maps with figurex.cartopy:
- cartopy
- If you want to make geographic maps with figurex.basemap:
- basemap >1.4
Related
- A discussion on GitHub/matplotlib actually requested this feature long ago.
- The project GitHub/contextplt has implemented a similar concept.
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 figurex-0.2.3.tar.gz
.
File metadata
- Download URL: figurex-0.2.3.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8965812bb49a4eea5488ff4171b10b99d15c07fee622fae4c868197c7c38713 |
|
MD5 | 1be8d8f4f932a303412772cfead0133d |
|
BLAKE2b-256 | c1b7622f0db72302264f9c27efeece7df214c81537f6ab953ae8d22387fb2d46 |
Provenance
File details
Details for the file figurex-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: figurex-0.2.3-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca1b780afbb30e904b3b0e914db115ae050e4c431f4be566f8fd1f3809a055f2 |
|
MD5 | cf314e01d2612a828ef4a6a78ba28ffd |
|
BLAKE2b-256 | a6f87d25a05561b8a06c6fc4011c985b80f747530aee6d5c003ce0b7d80e59cd |