1

The data:

x = 300, 300, 300, 300, 300

y = 1200, 900, 200, -600, -1371

I already plot with matplotlib using plt.plot(x, y, marker='s', linestyle='dotted') yet it just show one style.

Is it possible if I want to make the line plot with two different line styles, from (300,1200) to (200,300) with solid style and then the rest is dotted.

Please help. Thanks

2 Answers2

1

As I stated in the comments you will need to split up your data like so:

x_dotted = x[3:]
y_dotted = y[3:]

x_solid = x[:3]
y_solid = y[:3]

Then just call plt.plot with your desired parameters

GhandiFloss
  • 392
  • 1
  • 6
1

I think this answer is good for your question. I tried to modify it for line styles. For more information read about LineCollection.

import numpy as np
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt


x = np.array([300, 300, 300, 300, 300])
y = np.array([1200, 900, 200, -600, -1371])


points = np.array([x, y]).T.reshape(-1, 1, 2)


segments = np.concatenate([points[:3], points[-3:]], axis=1)
line_styles = [("dashed"),("solid")]

lc = LineCollection(segments, linestyles=line_styles, color='black')

fig,a = plt.subplots()
a.add_collection(lc)
a.set_xlim(0,500)
a.set_ylim(-1500,1500)
plt.show()

result