Wednesday, 27 September 2023

Show some part of the line as solid and other part of the line as dashed

In this post I am going to explain how to show some part of the line as solid line and other part as dashed line

 

For example, I have below data points.

 

x1 = [5, 10, 15, 20, 25, 30, 35, 40, 45]

y1 = [ 70, 235, 500, 865, 1330, 1895, 2560, 3325, 4190]

 

plt.plot(x1[:4], y1[:4], linestyle='-', color='b')

Above snippet draw a solid line from the first four data points.


plt.plot(x1[3:], y1[3:], linestyle='--', color='r')

Above snippet draw dashed line from the 3rd data point to last.

 

Using above statements together, we can address the problem statement.

solid_and_dashed_line.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25, 30, 35, 40, 45]
y1 = [ 70, 235, 500, 865, 1330, 1895, 2560, 3325, 4190]

plt.plot(x1[:4], y1[:4], linestyle='-', color='b')
plt.plot(x1[3:], y1[3:], linestyle='--', color='b')

# To display the legend on the plot
plt.legend()

plt.title('solid and dashed line mix')

plt.show()

Output



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment