问题描述
数据:
x = 300,300,300,300,300
y = 1200,900,200,-600,-1371
我已经使用plt.plot(x,y,marker ='s',linestyle ='dotted')使用matplotlib进行了绘制,但是它只显示了一种样式。
是否可以使用两种不同的线型(从(300,1200)到(200,300))以实心线型绘制线图,然后将其他点划线。
请帮助。谢谢
解决方法
正如我在评论中所述,您将需要像这样拆分数据:
x_dotted = x[3:]
y_dotted = y[3:]
x_solid = x[:3]
y_solid = y[:3]
然后只需使用所需参数调用plt.plot
,我认为this answer很适合您的问题。我试图将其修改为线条样式。有关更多信息,请参阅LineCollection。
import numpy as np
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
x = np.array([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()