You can download this code by clicking the button below.
This code is now available for download.
This function uses Matplotlib to plot a line graph and optionally adds scatter points to the plot.
Technology Stack : Matplotlib, NumPy
Code Type : Graphics drawing
Code Difficulty : Intermediate
import matplotlib.pyplot as plt
import numpy as np
def plot_line_with_scatter(x, y, scatter_points=None):
"""
This function plots a line graph and optionally adds scatter points to the plot.
Args:
x (array-like): The x values for the line.
y (array-like): The y values for the line.
scatter_points (tuple, optional): A tuple of arrays for x and y coordinates of scatter points.
"""
# Create a line plot
plt.plot(x, y, label='Line')
# If scatter points are provided, add them to the plot
if scatter_points:
plt.scatter(*scatter_points, color='red', label='Scatter Points')
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line with Scatter Plot')
# Show legend
plt.legend()
# Display the plot
plt.show()
# Example usage
x_values = np.linspace(0, 10, 100)
y_values = np.sin(x_values)
scatter_x = [1, 4, 7]
scatter_y = [0.5, -0.5, -0.8]
plot_line_with_scatter(x_values, y_values, (scatter_x, scatter_y))