pythonbeginner

Matplotlib Charts for Data Pipelines

Generate and save charts programmatically from pipeline output using matplotlib.

python
import matplotlib
matplotlib.use('Agg')  # non-interactive backend
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame({'month': pd.period_range('2024-01', periods=12, freq='M'),'revenue': np.random.randint(10000, 50000, 12)})

fig, axes = plt.subplots(1, 2, figsize=(12, 4))

# Bar chart
axes[0].bar(df['month'].astype(str), df['revenue'], color='steelblue')
axes[0].set_title('Monthly Revenue')
axes[0].tick_params(axis='x', rotation=45)

# Rolling trend
df['ma3'] = df['revenue'].rolling(3).mean()
axes[1].plot(df['month'].astype(str), df['revenue'], label='Revenue')
axes[1].plot(df['month'].astype(str), df['ma3'], label='3M MA', linestyle='--')
axes[1].legend()
axes[1].tick_params(axis='x', rotation=45)

plt.tight_layout()
plt.savefig('revenue_report.png', dpi=150)
print('Chart saved')

Use Cases

  • automated reports
  • pipeline visualisation
  • BI charting

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.