用多种线型绘制一条线

问题描述

数据:

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()

result

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...