How to draw multiple x-axes in Python
- User
How to draw multiple x-axes in Python
- Assistant
In general, adding multiple x-axes to a plot in Python can be accomplished by using the twinx() method from the matplotlib library. The twinx() method creates a twin y-axis that shares the same x-axis with the original plot, allowing multiple y-axes to be displayed on the same plot.
Here is an example code that demonstrates how to add multiple x-axes to a plot in Python:
import numpy as np import matplotlib.pyplot as plt # Generate some example data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Create a figure and axes object fig, ax1 = plt.subplots() # Plot the first data set on the first x-axis ax1.plot(x, y1, color='red') ax1.set_xlabel('x-axis 1') ax1.set_ylabel('y-axis 1', color='red') # Create a second y-axis that shares the same x-axis as the first axis ax2 = ax1.twinx() # Plot the second data set on the second x-axis ax2.plot(x, y2, color='blue') ax2.set_xlabel('x-axis 2') ax2.set_ylabel('y-axis 2', color='blue') # Add a legend for the two data sets ax1.legend(['y1'], loc='upper left') ax2.legend(['y2'], loc='upper right') # Display the plot plt.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.