generate python codes to plot scatter plot with multiple y axis
Private research annotation
Sign in to add a private research label or note.
- User
generate python codes to plot scatter plot with multiple y axis
- Assistant
To create a scatter plot with multiple y-axes in Python, you can use the
matplotliblibrary. Here's an example code that demonstrates how to create a scatter plot with two y-axes:pythonimport numpy as np import matplotlib.pyplot as plt # Generate some data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Create a figure and a set of subplots fig, ax1 = plt.subplots() # Plot the first set of data on the first y-axis ax1.scatter(x, y1, color='blue') ax1.set_xlabel('X data') ax1.set_ylabel('Y1 data', color='blue') ax1.tick_params(axis='y', labelcolor='blue') # Create a second y-axis and plot the second set of data on it ax2 = ax1.twinx() ax2.scatter(x, y2, color='red') ax2.set_ylabel('Y2 data', color='red') ax2.tick_params(axis='y', labelcolor='red') # Display the plot plt.show()In this example, we first generate some data using NumPy. We then create a figure and a set of subplots using
plt.subplots(). We plot the first set of data on the first y-axis usingax1.scatter()and set the x and y labels usingax1.set_xlabel()andax1.set_ylabel(). We also set the color of the y-axis labels and ticks to blue usingax1.tick_params().Next, we create a second y-axis using
ax1.twinx()and plot the second set of data on it usingax2.scatter(). We set the y-axis label to red usingax2.set_ylabel()and set the color of the y-axis labels and ticks to red usingax2.tick_params().Finally, we display the plot using
plt.show(). The resulting plot will have two y-axes, one on the left for the first set of data and one on the right for the second set of data. The scatter points for each set of data will be plotted on the corresponding y-axis. - User
also add x axis label
- Assistant
Sure, I can modify the previous example to include an x-axis label. Here's the updated code:
pythonimport numpy as np import matplotlib.pyplot as plt # Generate some data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Create a figure and a set of subplots fig, ax1 = plt.subplots() # Plot the first set of data on the first y-axis ax1.scatter(x, y1, color='blue') ax1.set_xlabel('X data') # Add x-axis label ax1.set_ylabel('Y1 data', color='blue') ax1.tick_params(axis='y', labelcolor='blue') # Create a second y-axis and plot the second set of data on it ax2 = ax1.twinx() ax2.scatter(x, y2, color='red') ax2.set_ylabel('Y2 data', color='red') ax2.tick_params(axis='y', labelcolor='red') # Display the plot plt.show()In this updated code, I added the line
ax1.set_xlabel('X data')to set the x-axis label. This line can be added anywhere after the creation of theax1object and before displaying the plot using . The resulting plot will now have an x-axis label in addition to the two y-axis labels.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.