线
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [3,4,6,7,3,2]
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x,y)
官方示例:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
为什么x轴的范围是0-3,而y轴的范围是1-4?
如果您提供要绘制的单个列表或数组,则matplotlib假定它是y值序列,并自动为您生成x值。 由于python范围从0开始,因此默认的x向量的长度与y相同,但从0开始。因此x数据为[0,1,2,3]。
Plot是一个多功能函数,可以接受任意数量的参数。 例如,要绘制x与y的关系图,可以这样写:
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
格式化绘图样式
对于每对x,y参数,都有一个可选的第三个参数,它是指示绘图颜色和线型的格式字符串。 格式字符串的字母和符号来自MATLAB,您可以将颜色字符串与线条样式字符串连接起来。 默认格式字符串是‘b-’,它是一条蓝色实线。 例如,要用红色圆圈绘制上面的内容,可以执行以下命令:
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
plt.plot()详细用法参见:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot.
plt.axis()详细用法参见:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axis.html#matplotlib.pyplot.axis.
使用数组在一个函数调用中绘制具有不同格式样式的几行:
import numpy as np
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
用关键字字符串绘图
在某些情况下,您具有某种格式的数据,该格式