A Python code to generate a number line image using matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm
# Path to your font file (e.g., Helvetica)
font_path = r'D:\Rahul\Websites\fonts\LUNCH.ttf'
# Create a FontProperties object
font_prop = fm.FontProperties(fname=font_path)
# Create a figure and an axis
fig, ax = plt.subplots(figsize=(10, 4))
# Define the range for the number line
min_value = -10
max_value = 10
# Create a number line with integers
x = np.arange(min_value, max_value + 1, 1)
# Plot the solid line for the number line
ax.plot([min_value, max_value], [0, 0], 'k-', linewidth=2) # Solid black line
# Plot the points on the number line
ax.plot(x, [0]*len(x), 'ko', markersize=5) # Points at integers
# Add arrows at the ends of the number line
ax.arrow(min_value, 0, -0.5, 0, head_width=0.2,
head_length=0.5, fc='black', ec='black')
ax.arrow(max_value, 0, 0.5, 0, head_width=0.2,
head_length=0.5, fc='black', ec='black')
# Add integer labels
font_family = 'LUNCH'
font_size = 14
font_weight = 'normal'
for i in x:
ax.text(i, 0.1, str(i), horizontalalignment='center', verticalalignment='bottom',
fontsize=font_size, fontfamily=font_family, fontweight=font_weight)
# Add "Origin" label at 0
ax.text(0, -0.8, 'Origin', horizontalalignment='center', verticalalignment='bottom',
fontsize=18, fontfamily=font_family, fontweight=font_weight,
color='darkgreen')
# Add an upward arrow at 0 between 0 and "Origin"
ax.arrow(0, -0.4, 0, 0.15, head_width=0.4,
head_length=0.05, fc='blue', ec='darkgreen')
# Add titles and subtitles
ax.text(0, 1.2, "Positive and Negative Integers", ha='center',
fontproperties=font_prop, fontsize=30)
# Set limits and hide axes
ax.set_xlim(min_value - 1, max_value + 1)
ax.set_ylim(-1, 2)
ax.axis('off')
# Display the plot
plt.show()
An improved version of the above code is below: