Matplotlib – Plot Solid Line

Matplotlib – Plot Solid Line Tutorial

In this tutorial, we’ll create a simple plot with a solid line using Matplotlib in Python.

Matplotlib - Plot Solid Line Example

1. Import Matplotlib.pyplot

Import the Matplotlib library, specifically the pyplot module.

import matplotlib.pyplot as plt

2. Create Data

Define the data points for the X and Y axes.

x = [1, 2, 3, 4, 5]
y = [20, 30, 55, 70, 60]

In this case, x represents the values on the X-axis, and y represents the corresponding values on the Y-axis.

3. Plot Solid Line

Use Matplotlib to plot a solid line.

plt.plot(x, y, linestyle='solid', marker='o')

linestyle: This parameter determines the style of the line in the plot.

When you set linestyle='solid', it indicates that the line connecting the data points will be represented with a solid line segments.

4. Customize and show the plot

Customize the plot with labels and titles, and then show the plot in a figure.

plt.title('Dashed Line Plot Example')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.show()

Complete program to plot Solid line using Matplotlib

Using all the above steps, let us write the complete program to draw a solid line using Matplotlib plot() function.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [20, 30, 55, 70, 60]

# Plot solid line
plt.plot(x, y, linestyle='solid', marker='o')

# Customize plot
plt.title('Solid Line Plot Example')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')

# Show the plot
plt.show()

Output

Matplotlib - Plot Solid Line Example

Summary

That concludes our tutorial on creating a plot with a dashed line using Matplotlib in Python!

Code copied to clipboard successfully 👍