The following produces a lot of rows of lines. I want the vertical gaps between the rows of lines to be much smaller though i.e. all the rows moved down apart from the lowest. I can’t for the life of me get my code to alter the vertical position though
import matplotlib.pyplot as plt
surfaces = [['sea ice', 'sea ice'],
['sea ice', 'sea ice', 'glacier'],
['sea ice', 'sea ice'],
['glacier', 'glacier', 'sea ice', 'sea ice']]
# Define colors for the surfaces
colors = {'sea ice': 'blue', 'glacier': 'red'}
# Set the linewidth
linewidth = 6
# Manually set the relative positions for each row with much closer gaps
relative_positions = [0.1, 0.12]
fig, ax = plt.subplots(figsize=(10, 4))
#spacing
for year, surface_list in enumerate(surfaces, start=2020):
y_offset = 0.1 # Reset y_offset at the start of each year
for surface in surface_list:
ax.plot([year - 0.4, year + 0.4], [y_offset, y_offset], color=colors[surface], linewidth=linewidth)
y_offset += 0.02 # Adjust to control gap size
ax.set_xlabel('Year')
ax.set_ylabel('colony')
ax.set_xticks([2020, 2021, 2022, 2023])
ax.set_xticklabels(['2020/21', '2021/22', '2022/23', '2023/24'])
ax.set_yticks([])
plt.show()
3
I added a line setting the ylim on suggestion of BigBen, this solved
ax.set_ylim(0, 0.15)