在matplotlib中以虚线更改破折号的间距

gro*_*ver 24 python plot matplotlib line subplot

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

plt.plot(x,y,linestyle='--')
Run Code Online (Sandbox Code Playgroud)

gca*_*tes 43

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

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
Run Code Online (Sandbox Code Playgroud)

线