Skip to content

Instantly share code, notes, and snippets.

@kolibril13
Created December 15, 2024 20:31
Show Gist options
  • Save kolibril13/750658068ee96e631dab00d1bc25e59b to your computer and use it in GitHub Desktop.
Save kolibril13/750658068ee96e631dab00d1bc25e59b to your computer and use it in GitHub Desktop.
matplotlib with environment variables.
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "matplotlib",
# ]
# ///
# hearder was generated with command:
# uv add --script foo.py matplotlib
# run with command:
# MY_COLOR=red uv run foo.py
import matplotlib.pyplot as plt
import numpy as np
import os
# Set color from environment variable or use default
color = os.getenv("MY_COLOR", "deepskyblue")
# Generate random data
np.random.seed(21)
x, y = 4 + np.random.normal(0, 2, 24), 4 + np.random.normal(0, 2, 24)
sizes, opacity = np.random.uniform(15, 80, 24), np.random.uniform(0, 1, 24)
# Create and display plot
plt.style.use("_mpl-gallery")
fig, ax = plt.subplots(figsize=(3, 3))
ax.scatter(x, y, s=sizes * 30, color=color, alpha=opacity)
ax.set(xlim=(0, 8), xticks=range(1, 8), ylim=(0, 8), yticks=range(1, 8))
plt.show()
@kolibril13
Copy link
Author

MY_COLOR=orange uv run https://gist.githubusercontent.com/kolibril13/750658068ee96e631dab00d1bc25e59b/raw/ecc002f8a0f5e14118941bf0f401c6db5518a053/foo.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment