Created
January 30, 2025 11:14
-
-
Save mthomason/e404fb03cc423aa1417dab1373e699fe to your computer and use it in GitHub Desktop.
Python 3 function to create a political quadrant chart. Uses `matplotlib`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import matplotlib.patches as patches | |
def create_quadrant_chart( | |
quadrant_labels, | |
quadrant_colors, | |
axis_labels, | |
arrow_props=None, | |
text_bbox=None, | |
figsize=(8, 8), | |
output_file=None | |
): | |
"""Create a customizable quadrant chart with arrows.""" | |
fig, ax = plt.subplots(figsize=figsize) | |
positions = [(0.25, 0.75), (0.75, 0.75), (0.25, 0.25), (0.75, 0.25)] | |
default_bbox = dict(facecolor='white', edgecolor='black', alpha=0.9) | |
# Draw colored quadrants | |
for (x, y), color in zip(positions, quadrant_colors): | |
rect = patches.Rectangle((x-0.25, y-0.25), 0.5, 0.5, | |
color=color, alpha=0.5) | |
ax.add_patch(rect) | |
# Add divider lines | |
ax.axhline(0.5, color='black', linewidth=1.5) | |
ax.axvline(0.5, color='black', linewidth=1.5) | |
# Add quadrant labels | |
for label, (x, y) in zip(quadrant_labels, positions): | |
ax.text(x, y, label, fontsize=10, ha='center', va='center', | |
bbox=text_bbox or default_bbox) | |
# Add arrows | |
arrow_args = dict(arrowstyle="->", lw=1.5, color='black') | |
if arrow_props: | |
arrow_args.update(arrow_props) | |
ax.annotate("", xy=(1.05, 0.5), xytext=(-0.05, 0.5), | |
arrowprops=arrow_args) # X-axis | |
ax.annotate("", xy=(0.5, 1.05), xytext=(0.5, -0.05), | |
arrowprops=arrow_args) # Y-axis | |
# Add axis labels | |
ax.text(1.1, 0.5, axis_labels['x_right'], fontsize=10, | |
va='center', ha='left') | |
ax.text(-0.1, 0.5, axis_labels['x_left'], fontsize=10, | |
va='center', ha='right') | |
ax.text(0.5, 1.1, axis_labels['y_top'], fontsize=10, | |
ha='center', va='bottom') | |
ax.text(0.5, -0.1, axis_labels['y_bottom'], fontsize=10, | |
ha='center', va='top') | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
ax.set_frame_on(False) | |
if output_file: | |
plt.savefig(output_file, dpi=300, bbox_inches='tight') | |
plt.show() | |
if __name__ == "__main__": | |
sample_config = { | |
'quadrant_labels': [ | |
"Authoritarianism\n(e.g...)", | |
"Right\n(e.g...)", | |
"Left\n(e.g...)", | |
"Libertarianism\n(e.g...)" | |
], | |
'quadrant_colors': ['lightcoral', 'lightblue', 'lightgreen', 'lightyellow'], | |
'axis_labels': { | |
'x_left': "Heavy Regulation External", | |
'x_right': "Light Regulation External", | |
'y_top': "Heavy Regulation Internal", | |
'y_bottom': "Light Regulation Internal" | |
} | |
} | |
create_quadrant_chart(**sample_config, output_file="sample_quadrant.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment