【问题标题】:Plot one line with multiple line styles用多种线型绘制一条线
【发布时间】:2020-12-27 17:31:38
【问题描述】:

数据:

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)用纯线风格制作线图,然后其余的都是虚线。

请帮忙。谢谢

【问题讨论】:

  • 您需要将数据拆分并绘制为单独的行

标签: python matplotlib styles line line-plot


【解决方案1】:

我认为this answer 很适合您的问题。我试图修改它的线条样式。更多信息请阅读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()

【讨论】:

    【解决方案2】:

    正如我在 cmets 中所说,您需要像这样拆分数据:

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

    然后用你想要的参数调用 plt.plot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-13
      • 2016-12-29
      • 2019-10-27
      • 1970-01-01
      • 2019-08-31
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多