本文代码源自官方实例,部分进行了修改和注解,增加不同线型的显示实例,帮助学习和查询。
"""
=======================================
plot图的线型设置以及自定义虚线段
=======================================
"""
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 500)
dashes = [10, 5, 100, 5] # 虚线参数,10和100是实点,5是虚点
fig, ax = plt.subplots()
line1, = ax.plot(x, np.sin(x), '--', linewidth=2, label='Dashesset retroactively')
line1.set_dashes(dashes) # 设置虚线参数
line2, = ax.plot(x,-1* np.sin(x), dashes=[30, 5, 10, 5], label='Dashes set proactively')
# 几种常用的线型的演示
line3, = ax.plot(x, np.sin(x)/2, ':', label='..style')
line4, = ax.plot(x,-np.sin(x)/2, '-.', label='-.style')
line5, = ax.plot(x,np.sin(x)/4, '--', label='--style')
line6, = ax.plot(x,-np.sin(x)/4, '^', label='--style')
ax.legend(loc='lowerright') # 显示图例,loc设置图例位置
plt.show()
运行结果: