在matplotlib中以虚线更改破折号的间距[复制]

问题描述 投票:19回答:1

这个问题在这里已有答案:

在Python中,使用matplotlib,有一种方法可以改变不同线条样式的破折号的距离,例如,使用以下命令:

plt.plot(x,y,linestyle='--')
python matplotlib plot line subplot
1个回答
38
投票

您可以使用plot命令中的dashes=(length, interval space)参数直接指定短划线长度/空格。

import matplotlib.pyplot as plt

fig,ax = plt.subplots()
ax.plot([0, 1], [0, 1], linestyle='--', dashes=(5, 1)) #length of 5, space of 1
ax.plot([0, 1], [0, 2], linestyle='--', dashes=(5, 5)) #length of 5, space of 5
ax.plot([0, 1], [0, 3], linestyle='--', dashes=(5, 10)) #length of 5, space of 10
ax.plot([0, 1], [0, 4], linestyle='--', dashes=(5, 20)) #length of 5, space of 20

lines

© www.soinside.com 2019 - 2024. All rights reserved.