Matplotlib 线形图

一、传数据的两种方式

matplotlib.pyplot.plot(args, scalex=True, scaley=True, data=None, kwargs)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.plot([2, 2, 2, 2])

plt.show()

在这里插入图片描述

fig, ax = plt.subplots()

data = {'a': [1, 2, 3], 'b': [3, 2, 1]}
ax.plot('a', 'b', data=data)

plt.show()

在这里插入图片描述

二、绘制多条线

fig, ax = plt.subplots()

data = {'a': [1, 2, 3], 'b': [3, 2, 1]}

ax.plot('a', 'b', data=data)
ax.plot([2, 2, 2, 2])

plt.show()

在这里插入图片描述

三、其它设置

1、颜色 —— color

可选值: ‘b’ blue ‘g’ green ‘r’ red ‘c’ cyan ‘m’ magenta ‘y’ yellow ‘k’ black ‘w’ white

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', drawstyle='steps')

plt.show()

在这里插入图片描述

2、线性 —— linestyle¶

可以使用字符串“ solid”,“ dotted”,“ dashed”或“ dashdot”定义简单的线型。通过提供破折号元组可以实现更精细的控制 。

可选值:[‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | ‘-’ | ‘–’ | ‘-.’ | ‘:’ | ‘None’ | ’ ’ | ‘’]
在这里插入图片描述

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', linestyle='dotted')

plt.show()

在这里插入图片描述

3、drawstyle

设置绘图的绘制样式。绘制样式确定点的连接方式。

参数:
可选值:[‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]

默认值:‘default’ 对于“默认”,这些点用直线连接。

阶梯变体将点与阶梯状线(即具有垂直阶梯的水平线)相连。它们在步骤的位置上有所不同:

  • ‘steps-pre’:步骤位于线段的开头,即线将位于右侧点的y值。
  • ‘steps-mid’:步骤在两点之间进行。
  • 'steps-post:步骤位于线段的末端,即线将位于左侧点的y值。
  • “ steps”等于“ steps-pre”,并且为了向后兼容而进行维护。
fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', drawstyle='steps')

plt.show()

在这里插入图片描述

4、标记 —— marker

可选参数:
在这里插入图片描述

点击查看更多参数

None是默认值,表示“无”,但是从其他文档中引用了此表以获取标记输入的有效输入,在这些情况下,None仍表示“默认值”。

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], marker='o')

plt.show()

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章