I am using python and matplotlib to try and recreate the dual axis in this chart. In Matplotlib how can I adjust the vertical position of each y axis?
Here is the code for a dual axis line chart:
import matplotlib.pyplot as plt
# Data for the x-axis
x = [1, 2, 3, 4, 5]
# Data for the primary y-axis
y1 = [10, 15, 7, 12, 9]
# Data for the secondary y-axis
y2 = [200, 150, 120, 180, 220]
# Create figure and axes objects
fig, ax1 = plt.subplots(figsize=(14,5))
# Plot the data for the primary y-axis
ax1.plot(x, y1, 'g-', label='Primary')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Primary Y-axis')
ax1.tick_params('y', colors='black')
# Create a second axes that shares the same x-axis
ax2 = ax1.twinx()
# Plot the data for the secondary y-axis
ax2.plot(x, y2, 'b-', label='Secondary')
ax2.set_ylabel('Secondary Y-axis')
ax2.tick_params('y', colors='black')
# Add a legend
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper right')
# Set the title and display the chart
plt.title('Dual Axis Line Chart')
plt.show()
Any ideas how I can modify this code to separate each line?