Skip to main content
 首页 » 编程设计

python matplotlib图例线型 '---'

2025年04月02日9jackei

我需要一种方法来制作 matplotlib 线型“---”。 3 个“-”。

character description 
'-'       solid line style 
'--'      dashed line style 
'-.'      dash-dot line style 
':'       dotted line style 
etc. 

我可以在列表中看到“-”和“--”,但在右侧,我的图例显示为“-- 红色虚线”(如果我写 linestyle = '--')。我想在我的图例框上使用“--- 红色虚线”

有什么方法可以让图例显示三个破折号? 这就是我正在做的事情。

import matplotlib.pyplot as mpt 
 
def main(): 
    mpt.title("hi") 
    mpt.xlabel("x axis") 
    mpt.ylim([0,50]) 
    mpt.xlim([0,10]) 
    mpt.ylabel("y axis") 
    mpt.plot([1,2,3,4],[2,3,4,5],'r', linestyle = '???????') 
    mpt.legend(["red dotted line"]) 
    mpt.show() 
 
main() 

请您参考如下方法:

使用 mpt.legend(handlelength=3) 和 linestyle='--'

mpt.plot([1,2,3,4],[2,3,4,5],'r', linestyle='--') 
mpt.legend(["red dotted line"], handlelength=3)