小白教程
所有教程
关于
Search
172.69.58.253
172.69.58.253
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Matplotlib 绘图线”的源代码
本页内容
上一节:
Matplotlib_绘图标记
下一节:
Matplotlib_轴标签和标题
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Matplotlib 绘图线}}[[Category:Matplotlib 教程|5]] = Matplotlib 绘图线 = 绘图过程如果我们自定义线的样式,包括线的类型、颜色和大小等。 === 线的类型 === 线的类型可以使用 '''linestyle''' 参数来定义,简写为 ls。 {| class="table table-striped table-hover" ! 类型 ! 简写 ! 说明 |- | 'solid' (默认) | '-' | 实线 |- | 'dotted' | ':' | 点虚线 |- | 'dashed' | '--' | 破折线 |- | 'dashdot' | '-.' | 点划线 |- | 'None' | '' 或 ' ' | 不画线 |} <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, linestyle = 'dotted') plt.show() </sample> 显示结果如下: 使用简写: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, ls = '-.') plt.show() </sample> 显示结果如下: === 线的颜色 === 线的颜色可以使用 '''color''' 参数来定义,简写为 c。 颜色类型: {| class="table table-striped table-hover" ! 颜色标记 ! 描述 ! |- | 'r' | 红色 | |- | 'g' | 绿色 | |- | 'b' | 蓝色 | |- | 'c' | 青色 | |- | 'm' | 品红 | |- | 'y' | 黄色 | |- | 'k' | 黑色 | |- | 'w' | 白色 | |} 当然也可以自定义颜色类型,例如:'''SeaGreen、#8FBC8F''' 等,完整样式可以参考 [https://www.xiaobai.wang/html/html-colorvalues.html HTML 颜色值]。 <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, color = 'r') plt.show() </sample> 显示结果如下: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, c = '#8FBC8F') plt.show() </sample> 显示结果如下: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, c = 'SeaGreen') plt.show() </sample> 显示结果如下: === 线的宽度 === 线的宽度可以使用 '''linewidth''' 参数来定义,简写为 lw,值可以是浮点数,如:'''1'''、'''2.0'''、'''5.67''' 等。 <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np ypoints = np.array([6, 2, 13, 10]) plt.plot(ypoints, linewidth = '12.5') plt.show() </sample> 显示结果如下: === 多条线 === plot() 方法中可以包含多对 x,y 值来绘制多条线。 <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np y1 = np.array([3, 7, 5, 9]) y2 = np.array([6, 2, 13, 10]) plt.plot(y1) plt.plot(y2) plt.show() </sample> 从上图可以看出 '''x''' 的值默认设置为 '''[0, 1, 2, 3]'''。 显示结果如下: 我们也可以自己设置 x 坐标等值: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x1 = np.array([0, 1, 2, 3]) y1 = np.array([3, 7, 5, 9]) x2 = np.array([0, 1, 2, 3]) y2 = np.array([6, 2, 13, 10]) plt.plot(x1, y1, x2, y2) plt.show() </sample> 显示结果如下:
返回至“
Matplotlib 绘图线
”。
上一节:
Matplotlib_绘图标记
下一节:
Matplotlib_轴标签和标题