elasticity.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Elasticity calculations for logistic x50 and s50."""
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from .core import (
  5. elasticity_x50_s50,
  6. elasticity_table_4panels,
  7. plot_x50_values,
  8. plot_s50_values,
  9. plot_x50_theta1_elasticity,
  10. plot_s50_theta1_elasticity,
  11. )
  12. def plot_combined_elasticity(table):
  13. """Plot all logistic coefficient elasticities in one two-panel figure."""
  14. labels = table["Panel"].tolist()
  15. x = np.arange(2)
  16. width = 0.17
  17. offsets = np.linspace(-1.5, 1.5, len(labels)) * width
  18. x50_values = table[["E_x50_theta0", "E_x50_theta1"]].abs().to_numpy(float)
  19. s50_values = table[["E_s50_theta0", "E_s50_theta1"]].abs().to_numpy(float)
  20. fig, axes = plt.subplots(1, 2, figsize=(10, 4.2), dpi=180)
  21. for index, label in enumerate(labels):
  22. axes[0].bar(x + offsets[index], x50_values[index], width=width, label=label)
  23. axes[1].bar(x + offsets[index], s50_values[index], width=width, label=label)
  24. axes[0].set_xticks(x, [r"$|\mathcal{E}_{\theta_0}(x_{50})|$", r"$|\mathcal{E}_{\theta_1}(x_{50})|$"])
  25. axes[1].set_xticks(x, [r"$|\mathcal{E}_{\theta_0}(s_{50})|$", r"$|\mathcal{E}_{\theta_1}(s_{50})|$"])
  26. axes[0].set_ylabel("Absolute elasticity")
  27. for label, axis in zip(("A", "B"), axes):
  28. axis.text(0.03, 0.95, label, transform=axis.transAxes, ha="left", va="top", fontsize=15)
  29. axis.grid(alpha=0.25, axis="y")
  30. axes[1].legend(frameon=True, fontsize=9, loc="upper left")
  31. fig.tight_layout()
  32. return fig, axes