Write a piece of code to display a scatterplot in python
- User
Write a piece of code to display a scatterplot in python
Assistant
Here's a complete example of how to create and display a scatterplot in Python using Matplotlib:
import matplotlib.pyplot as plt import numpy as np # Generate random data x = np.random.rand(50) y = np.random.rand(50) # Create scatterplot plt.figure(figsize=(10, 6)) plt.scatter(x, y, color='blue', alpha=0.6) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatterplot Example') plt.grid(True) plt.show()This code generates random data for the x and y coordinates, creates a scatterplot with blue points that are slightly transparent (alpha=0.6), adds labels to the axes, a title, and a grid for better readability. The
plt.figure(figsize=(10, 6))line sets the size of the figure to 10 inches by 6 inches. Finally,plt.show()displays the plot.If you want a more customized scatterplot, you can vary the colors and sizes of the points based on additional data. Here's an example of that:
import matplotlib.pyplot as plt import numpy as np # Generate random data np.random.seed(42) # For reproducibility x = np.random.rand(50) y = np.random.rand(50) colors = np.random.rand(50) sizes = 1000 * np.random.rand(50) # Create scatterplot plt.figure(figsize=(10, 6)) plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Customized Scatterplot Example') plt.colorbar(label='Color Intensity') plt.grid(True) plt.show()In this version, the points have colors and sizes based on additional random data, and a color bar is added to show the scale of the colors. The
cmap='viridis'sets the colormap to 'viridis', which is a perceptually uniform colormap. Thealpha=0.5makes the points semi-transparent, which can help with overplotting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.