code that creates dashed versions of an existing curve and a straight line

PHOTO EMBED

Tue Nov 14 2023 20:17:41 GMT+0000 (Coordinated Universal Time)

Saved by @dr_dziedzorm

import matplotlib.pyplot as plt
import numpy as np

# Function to plot a dashed curve from given x and y values
def plot_dashed_curve(x, y, label):
    plt.plot(x, y, 'r--', label=label, dashes=(5, 5))

# Function to plot a dashed line from given x values, slope, and intercept
def plot_dashed_line(x, m, c, label):
    y = m * x + c
    plt.plot(x, y, 'b--', label=label, dashes=(5, 5))

# Generate a range of x values for the existing line and curve
x_values = np.linspace(-10, 10, 100)

# Define the existing curve (e.g., a parabola)
y_curve = -x_values**2 + 10
# Plot the dashed curve
plot_dashed_curve(x_values, y_curve, 'Dashed Curve: Parabola')

# Define the existing line (e.g., y = x)
m = 1  # slope
c = 0  # y-intercept
# Plot the dashed line
plot_dashed_line(x_values, m, c, 'Dashed Line: y = x')

# Add title and labels
plt.title('Dashed Line and Curve')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)

# Show the plot
plt.show()
content_copyCOPY