可以使用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
下图是一个实例,一个是dashes(15,1),另一个是dashes(5,1),如下图所示
需要设置图例时